This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Problem while working with JPopupMenu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem while working with JPopupMenu" Watch "Problem while working with JPopupMenu" New topic
Author

Problem while working with JPopupMenu

nirupama singhal
Greenhorn

Joined: Feb 23, 2001
Posts: 17
Hi!!
I am working with JPopupMenu. But the probelm is Popupmenu cannot able to listen keyboards events.what may be the error? can some ont throw some light on my problem.
Thanks
nirupama
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
JPopupMenu inherits from JComponent, which inherits from Container, which inherits from Component. The Component class includes a method addKeyListener(), so you should be able to make this work.
nirupama singhal
Greenhorn

Joined: Feb 23, 2001
Posts: 17
Well Bodie, as per ur suggestion i tried addKeyListener() also but JPopupMenu is not listening any key board events even navigation keys . what may be the problem. pls suggest something
thanks
nirupama
Originally posted by Bodie Minster:
JPopupMenu inherits from JComponent, which inherits from Container, which inherits from Component. The Component class includes a method addKeyListener(), so you should be able to make this work.

Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Perhaps you can post your code, and we can see why it is not listening.


"JavaRanch, where the deer and the Certified play" - David O'Meara
nirupama singhal
Greenhorn

Joined: Feb 23, 2001
Posts: 17
Well Bodie
the code is given below. pls check it out where the problem is
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.plaf.basic.*;
public class PopupMenuExample extends JPanel implements ActionListener, PopupMenuListener , MouseListener,KeyListener
{
public JPopupMenu popup;
public PopupMenuExample()
{
popup = new JPopupMenu();
JMenuItem item;
popup.add(item = new JMenuItem("Left"));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(this);

popup.add(item = new JMenuItem("Center"));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(this);

item.setMnemonic(KeyEvent.VK_T);
popup.add(item = new JMenuItem("Right"));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(this);

popup.add(new JMenuItem("Full"));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(this);

popup.addSeparator();
popup.add(item = new JMenuItem("Settings ..."));
item.addActionListener(this);

popup.setLabel("Justification");
popup.setBorder(new BevelBorder(BevelBorder.RAISED));
popup.addPopupMenuListener(this);
addMouseListener(this);
popup.addKeyListener(this);


}
public void mousePressed(MouseEvent e)
{
checkPopup(e);
}
public void mouseClicked(MouseEvent e)
{
checkPopup(e);
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
checkPopup(e);
}

private void checkPopup(MouseEvent e)
{
if(e.isPopupTrigger())
{
popup.show(this, e.getX(), e.getY());
}
}
public void popupMenuWillBecomeVisible(PopupMenuEvent e)
{
System.out.println("popup menu will be visible!");
}

public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
{
System.out.println("popup menu will be invisible!");
}
public void popupMenuCanceled(PopupMenuEvent e)
{
System.out.println("popup menu is hidden!");
}

public void actionPerformed(ActionEvent event)
{
System.out.println("Popup menu item["+event.getActionCommand()+"]was pressed.!");
}
public void keyTyped(KeyEvent e)
{
System.out.println("key typed");
}
public void keyReleased(KeyEvent e)
{
System.out.println("key released");
}
public void keyPressed(KeyEvent e)
{
System.out.println("key 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);
}
Manish Gupta
Greenhorn

Joined: Jul 23, 2001
Posts: 1
Hi Nirupama
I am also facing the similar problem did you got any solution.
(Problem while working with JPopupMenu - Keys not working)
Regards
Mansih
Vijay Thiagarajan
Greenhorn

Joined: Aug 27, 2002
Posts: 2
Hi Did you people find an answer for the keyboard navigation problem with Jpopupmenu
I am having a JPopupmenu with a Jtree in it when the popup menu goes outside the container the keyboard does not work unless explicity selected with left click
Any ideas please
Reply me at vijaythiagarajan@yahoo.com


Vijay Thiagarajan<br />Software Engineer<br />i7 Software Asia Pvt Ltd <br />Chennai, India,<br />cell: 98400 54984
Vijay Thiagarajan
Greenhorn

Joined: Aug 27, 2002
Posts: 2
refere suns Bug ID 4212563
it seems to say the keyboard navigation is not possible
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem while working with JPopupMenu