Path: csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.gui Subject: Re: Passing KeyEvent upward Date: Sun, 22 Jul 2012 17:39:13 -0700 Organization: A noiseless patient Spider Lines: 70 Message-ID: References: <0f05834e-c654-4925-b78a-b8328b23ba0b@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 23 Jul 2012 00:39:15 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="61282af8d6595e8d991edb5ac03d6e00"; logging-data="4333"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+NQZadVpjjqJE8RPfevf1H09HIVB9PiBU=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20120713 Thunderbird/14.0 In-Reply-To: Cancel-Lock: sha1:yqTYChlknvdH7Gc28HI/qhyGlJE= Xref: csiph.com comp.lang.java.gui:5198 On 7/22/2012 5:26 PM, markspace wrote: > Sounds like an accelerator to me. Here's the simplest example I could think of. Tell me if it works like what you want. package quicktest; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JTextArea; import javax.swing.KeyStroke; import javax.swing.SwingUtilities; /** * * @author Brenden */ public class Accelerator { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame(); JTextArea ta = new JTextArea(); frame.add(ta); JMenuBar mbar = new JMenuBar(); JMenu file = new JMenu("File"); mbar.add(file); JMenuItem blarg = new JMenuItem("blarg"); blarg.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_1, ActionEvent.ALT_MASK)); blarg.addActionListener( new Blarg( ta ) ); file.add(blarg); frame.setJMenuBar(mbar); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize( 400, 400 ); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } private static class Blarg implements ActionListener { private final JTextArea ta; public Blarg(JTextArea ta) { this.ta = ta; } @Override public void actionPerformed(ActionEvent e) { ta.append( " blarg" ); } } }