• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem while working with JPopupMenu

 
nirupama singhal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you can post your code, and we can see why it is not listening.
 
nirupama singhal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
refere suns Bug ID 4212563
it seems to say the keyboard navigation is not possible
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic