Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #4180

Re: Detect when menu is c

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 "izittm" <izittm@THRWHITE.remove-dii-this>
Subject Re: Detect when menu is c
Message-ID <ed522ffd-eb04-440b-9a67-c250d3df8f8a@f36g2000hsa.googlegroups.com> (permalink)
X-Comment-To comp.lang.java.gui
Newsgroups comp.lang.java.gui
In-Reply-To <nospam-69A228.15040809092008@news.motzarell
References <nospam-69A228.15040809092008@news.motzarell
Content-Type text/plain; charset=IBM437
Content-Transfer-Encoding 8bit
X-Gateway time.synchro.net [Synchronet 3.15a-Win32 NewsLink 1.92]
Lines 82
Date Wed, 27 Apr 2011 15:49:09 GMT
NNTP-Posting-Host 96.60.20.240
X-Complaints-To news@tds.net
X-Trace newsreading01.news.tds.net 1303919349 96.60.20.240 (Wed, 27 Apr 2011 10:49:09 CDT)
NNTP-Posting-Date Wed, 27 Apr 2011 10:49:09 CDT
Organization TDS.net
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.gui:4180

Show key headers only | View raw


  To: comp.lang.java.gui
Sure, should have done that at the beginning. Here's what I tried:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MenuPrb {
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JMenuBar jmb = new JMenuBar();
		frame.add(jmb);
		JMenu menu1 = new JMenu("menu1");
		jmb.add(menu1);

		JMenu menu2 = new JMenu("menu2");
		menu1.add(menu2);
		menu2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("menu2: actionPerformed");
			}
		});
		menu2.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				System.out.println("menu2: mouseClicked");
			}
		});
		menu2.addKeyListener(new KeyListener() {
			public void keyPressed(KeyEvent e) {
				System.out.println("menu2: keyPressed");
			}
			public void keyReleased(KeyEvent e) {
				System.out.println("menu2: keyReleased");
			}
			public void keyTyped(KeyEvent e) {
				System.out.println("menu2: keyTyped");
			}
		});

		for(int i = 0; i < 10; i++) {
			JMenuItem mi = new JMenuItem("menu item " + i);
			menu2.add(mi);
			mi.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.out.println("mi: actionPerformed");
				}
			});
		}

		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

If you click on any of the menu items, it works. If you click on
menu2, it doesn't. That is what I would like to achieve - get clicks
from menu2. It can be done by menu2.addMouseListener(...) as above,
but that won't catch keyboard activation. menu2.addKeyListener doesn't
work also, as you can see - none of the keys pressed over menu2
trigger either action or key event handlers. Enter works on menu items
by triggering action events (in my opinion correct behavior).

Notice that in both links you provided (I've already read the first
one) they never set any listeners on JMenu, only on JMenuItem. Maybe
this is not what they had in mind, but I consider this quite oddly
designed in that case. After all, JMenu is a descendant of
AbstractButton. I'd expect a button to be clickable... Clickable in
the same way as other buttons - by mouse & keyboard, yielding
actionPerformed events.

---
 * 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

Back to comp.lang.java.gui | Previous | Next | Find similar


Thread

Re: Detect when menu is c "izittm" <izittm@THRWHITE.remove-dii-this> - 2011-04-27 15:49 +0000

csiph-web