Confused with JPopupMenu class
Hello Friend,
I need you help to get solution over one of may java query. Currently I am studying the class concept of java programming. In which the 'JPopupMenu class' is complex class of java. I am not able to get functionality of 'JPopupMenu class' of java. I also don't have any idea about it's constructors. If you have sound knowledge about the JPopupMenu class of java, then please let me know the same. Your help would be greatly appreciated.
Re: Confused with JPopupMenu class
Hi,
A JPopupMenu class is used for an implementation of a popup menu - a small window that pops up and displays a series of choices. A JPopupMenu is used for the menu that appears when the user selects an item on the menu bar. It is also used for "pull-right" menu that appears when the selects a menu item that activates it. Finally, a JPopupMenu can also be used anywhere else you want a menu to appear. For example, when the user right-clicks in a specified area.
Re: Confused with JPopupMenu class
Example of JPopupMenu class of java:
Code:
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class JPopupMenuDM {
public static void main(final String args[]) {
JFrame frame = new JFrame("Popup Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextField textField01 = new JTextField();
frame.add(textField01, BorderLayout.NORTH);
final JPopupMenu popup = new JPopupMenu();
JMenuItem menuItem01 = new JMenuItem("Option 1");
popup.add(menuItem01);
JMenuItem menuItem02 = new JMenuItem("Option 2");
popup.add(menuItem02);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
popup.show(textField01, 10,10);
} };
KeyStroke keystroke1 =
KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0, false);
textField01.registerKeyboardAction(actionListener, keystroke1,
JComponent.WHEN_FOCUSED);
frame1.setSize(350, 150);
frame2.setVisible(true);
}
}
Re: Confused with JPopupMenu class
Hi friend,
Please refer following some important methods of the JPopupMenu class:
- insert(Component compt, int indx)
- getComponentIndex(Component compt)
- createActionComponent(Action actnc)
- createActionChangeListener(JMenuItem mnt)
- addPopupMenuListener(PopupMenuListener lstn)
- add(JMenuItem mtn)
Re: Confused with JPopupMenu class
In following example of JPopupMenu class, I have used the 'getPopupMenu()' method of the JPopupMenu class. Please review it carefully:
Code:
import javax.swing.JMenu;
javax.swing.JPopupMenu
import javax.swing.JMenuItem;
public class JPopupMenuMain
{
public static void main(String[] argvb) throws Exception
{
JMenu menuX = new JMenu("Menu Label");
menuX.add(new JMenuItem("Item Label"));
boolean lwPopupX = menu.getPopupMenu().isLightWeightPopupEnabled(); // true
menuX.getPopupMenu().setLightWeightPopupEnabled(false);
}
}
Re: Confused with JPopupMenu class
Hi,
There are six nested class contained in the JPopupMenu class which are mentioned below:
- Container.AccessibleAWTContainer
- Component.AccessibleAWTComponent
- JPopupMenu.Separator
- JComponent.AccessibleJComponent
- Component.FlipBufferStrategy
- JPopupMenu.AccessibleJPopupMenu