Hello Friends Can any body throw some light why key board events is not listening to the following code. i also tried applying KeyListener to the PopupMenu but still it didn't help. Thanx is this regards Nirupama The code is-- import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class PopupMenuExample extends JPanel implements ActionListener, PopupMenuListener , MouseListener { public JPopupMenu popup; public PopupMenuExample() { popup = new JPopupMenu(); JMenuItem item; popup.add(item = new JMenuItem("Left")); item.setHorizontalTextPosition(JMenuItem.LEFT); item.addActionListener(this);
popup.add(item = new JMenuItem("Center")); item.setHorizontalTextPosition(JMenuItem.CENTER); item.addActionListener(this);
popup.add(item = new JMenuItem("Right")); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(this);
} public void mousePressed(MouseEvent event) { checkPopup(event); } public void mouseClicked(MouseEvent event) { checkPopup(event); } public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { } public void mouseReleased(MouseEvent event) { checkPopup(event); }
private void checkPopup(MouseEvent event) { if(event.isPopupTrigger()) { popup.show(this, event.getX(), event.getY()); } } public void popupMenuWillBecomeVisible(PopupMenuEvent event) { System.out.println("popup menu will be visible!"); }
public void popupMenuWillBecomeInvisible(PopupMenuEvent event) { System.out.println("popup menu will be invisible!"); } public void popupMenuCanceled(PopupMenuEvent event) { System.out.println("popup menu is hidden!"); }
public void actionPerformed(ActionEvent event) { System.out.println("Popup menu item["+event.getActionCommand()+"]was pressed.!"); } public static void main(String s[]) { JFrame frame = new JFrame("popup menu example"); frame.setContentPane(new PopupMenuExample()); frame.setSize(300,300); frame.setVisible(true); } }
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
Mouse events work just fine for me. Got a Frame. Right click brings up your beautiful popup menu. messages are displayed to dos. What isn't working for you?
"JavaRanch, where the deer and the Certified play" - David O'Meara
nirupama singhal
Greenhorn
Joined: Feb 23, 2001
Posts: 17
posted
0
Hello Cindy, Firstly Thank u so much 4 the nice reply. ya as u said the popup is invoked 'n' its working too fine with mouse events. But the popup menu is not listening to key board events like up arrow, down arrow key or enter . these key board events work too fine with JMenu, PopupMenu but not with JPopupMenu. I am not able to understand whats the problem with my code. Regards Nirupama
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
Well, when I run your code, and right click to bring up the JPopupMenu, I can use my arrow keys to move the selection up and down the list just fine. If I move the selection using the arrow keys and hit enter, it correctly knows which item was selected. If I use the mouse to select an item, this also works just fine. So I am still not sure what is not working for you.
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
You know, the one thing that is funny about your code is the contentPane. You are setting the contentPane to your PopupMenuExample, when normally one adds the PopupMenuExample container to the contentPane that was set in the constructor for the JFrame. frame.getContentPane().add(new PopupMenuExample()); However, both approaches seem to work just fine - sooooooo?
nirupama singhal
Greenhorn
Joined: Feb 23, 2001
Posts: 17
posted
0
Hello Cindy Firstly i must thanks 4 the reply 'n' telling about my mistake. Cindy , I tried again but the problem is same, not listening to key board events with this code.its listening to mouse events but not key board events Thanx Nirupama
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
So maybe it is just the "keyboard" that you have assigned in you operating system. In windows you can change the keyboard definition to make the keys work differently. It's a thought. What operating system are you on. KeyEvent bugs in java are OS dependant.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: KeyBoard events not listening in JPopupMenu