Path: csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!news.glorb.com!news-out.readnews.com!news-xxxfer.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!86597e80!not-for-mail From: "Chameleon" Subject: Re: menu accelerator Message-ID: X-Comment-To: comp.lang.java.gui Newsgroups: comp.lang.java.gui In-Reply-To: <46caa9a5$0$21098$da0feed9@news.zen.co.uk> References: <46caa9a5$0$21098$da0feed9@news.zen.co.uk> Content-Type: text/plain; charset=IBM437 Content-Transfer-Encoding: 8bit X-Gateway: time.synchro.net [Synchronet 3.15a-Win32 NewsLink 1.92] Lines: 102 Date: Wed, 27 Apr 2011 15:38:17 GMT NNTP-Posting-Host: 96.60.20.240 X-Complaints-To: news@tds.net X-Trace: newsreading01.news.tds.net 1303918697 96.60.20.240 (Wed, 27 Apr 2011 10:38:17 CDT) NNTP-Posting-Date: Wed, 27 Apr 2011 10:38:17 CDT Organization: TDS.net Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.gui:2296 To: comp.lang.java.gui > Chameleon wrote: >> Andrew Thompson wrote: >>> >>> Paste an SSCCE here and we >>> might be able to see what you are doing wrong, >> >> I am trying to say, how can I assign the "F1" accelerator to a >> menuitem. The following code doesn't work: >> --------------------- >> JMenuBar jmb = new JMenuBar(); >> JMenu jm = new JMenu("Help"); >> JMenuItem jmi = new JMenuItem("Help Index"); >> jmi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0)); >> jmi.addActionListener(listener); >> jm.add(jmi); >> jmb.add(jm); >> --------------------- >> I don't want "Shift+F1", "Ctrl+F1", etc. I want "F1". >> > > Thats not an SSCCE! > > You have a bug in the code you are *not* showing us. > > Here's a working SSCCE based on your snippet: > -----------------------------------------8<----------------------------------- > > import java.awt.event.ActionEvent; > import java.awt.event.ActionListener; > import java.awt.event.KeyEvent; > > import javax.swing.JFrame; > import javax.swing.JLabel; > import javax.swing.JMenu; > import javax.swing.JMenuBar; > import javax.swing.JMenuItem; > import javax.swing.JPanel; > import javax.swing.KeyStroke; > import javax.swing.SwingUtilities; > > public class TestAccellerator implements ActionListener { > > TestAccellerator() { > > // So I don't need to edit Chameleon's code at all > ActionListener listener = this; // ick. > > // Chameleon's snippet *exactly* as in newsgroup > JMenuBar jmb = new JMenuBar(); > JMenu jm = new JMenu("Help"); > JMenuItem jmi = new JMenuItem("Help Index"); > jmi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0)); > jmi.addActionListener(listener); > jm.add(jmi); > jmb.add(jm); > // end of Chameleon's snippet > > JPanel p = new JPanel(); > p.add(new JLabel("Panel")); > > JFrame f = new JFrame("Test Accellerator"); > f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); > f.add(p); > f.setJMenuBar(jmb); > f.pack(); > f.setVisible(true); > } > > public void actionPerformed(ActionEvent e) { > System.out.printf("Event: command '%s' from object '%s'\n", e > .getActionCommand(), e.getSource()); > } > > public static void main(String[] args) { > SwingUtilities.invokeLater(new Runnable() { > public void run() { > new TestAccellerator(); > } > }); > } > > } > > -----------------------------------------8<----------------------------------- > > When I press F1 I get the output I expected. I dont know what is SSCCE but now I know. Holy Sh*t! F1 working! I am dump and I spend your time for nothing! I found a clause "if (key == 0 || modifier == 0) ..." in my code... What a SHAME. Sorry. --- * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet! --- Synchronet 3.15a-Win32 NewsLink 1.92 Time Warp of the Future BBS - telnet://time.synchro.net:24