Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Daniele Futtorovic Newsgroups: comp.lang.java.gui Subject: Re: Passing KeyEvent upward Date: Wed, 25 Jul 2012 00:32:20 +0200 Organization: A noiseless patient Spider Lines: 105 Message-ID: References: <0f05834e-c654-4925-b78a-b8328b23ba0b@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Injection-Date: Tue, 24 Jul 2012 22:32:31 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="ca57181390b9dc8db08ce952559e403c"; logging-data="31424"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ioOXdklq+3L9YAilrluYV" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.28) Gecko/20120306 Thunderbird/3.1.20 In-Reply-To: <0f05834e-c654-4925-b78a-b8328b23ba0b@googlegroups.com> Cancel-Lock: sha1:+uzPofRM7WueTEKoOh5Bm1yxJ2M= Xref: csiph.com comp.lang.java.gui:5204 On 23/07/2012 01:09, Adam Beneschan allegedly wrote: > Hi, > > I'm trying to learn how to program in Java and use some of the GUI features.. Here's something I'm trying to do but haven't succeeded at yet. > > I have a text window (JTextPane) object, and I want to set things up so that when a certain something happens, a popup menu (JPopupMenu) will come up. But I'd like it so that if the user presses a key, the key is handled just as if the menu hadn't come up--that is, the key-press event will be handled by the JTextPane (or is it the associated document?) as if the user had typed that key into the document. I've already set up a MenuKeyListener for the popup menu that recognizes the KeyEvent and calls menu.setVisible(false) to get rid of the menu. So far, so good. But I don't know how to get the KeyEvent propagated to the text window. I've tried getKeyListeners() on the JTextPane and then calling the keyTyped explicitly on each listener. No good--the only listener it found was one I had added myself for debugging. I also tried textPane.getInputContext().dispatchEvent(e) on the KeyEvent; that didn't do what I wanted either (seemed to have no effect). > > Any thoughts? I haven't found anything else to try. > > Thanks, > > -- Adam The following appears to be working, although not necessarily optimal: package scratch; import java.awt.EventQueue; import java.awt.event.KeyEvent; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPopupMenu; import javax.swing.JTextArea; public class ScratchUI { public static void main( String[] args ) { EventQueue.invokeLater( new Runnable(){ public void run(){ JFrame f = new JFrame("Gah"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextArea c = new Area(); c.setColumns(40); c.setRows(10); c.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder(10,20,10,20), BorderFactory.createTitledBorder("Type here:") ) ); f.getContentPane().add( c ); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } } ); } static boolean isShowMenuEvent( KeyEvent e ){ return ! e.isConsumed() && e.getID() == KeyEvent.KEY_PRESSED && (e.getModifiers() == 0 || e.getModifiersEx() == KeyEvent.SHIFT_DOWN_MASK) && e.getKeyCode() == KeyEvent.VK_A ; } static final class Area extends JTextArea { private JPopupMenu menu; protected JPopupMenu createMenu(){ JPopupMenu ret = new JPopupMenu(); ret.add("Blah"); ret.add("Bleh"); ret.addSeparator(); ret.add("Uh."); ret.setFocusable(false); ret.setFocusTraversalKeysEnabled(false); return ret; } private void toggleShowMenu( KeyEvent e ){ if( menu == null ){ menu = createMenu(); } if( menu.isShowing() ){ menu.setVisible(false); } else { //position?? menu.show(e.getComponent(), e.getComponent().getX() + 10, e.getComponent().getY() + 50); } } @Override protected void processKeyEvent( KeyEvent e ) { if( isShowMenuEvent(e) ){ toggleShowMenu(e); } super.processKeyEvent(e); } } } HTH, -- DF.