Saturday, August 24, 2013

JPopupMenu

JPopupMenu represents a popup menu. A popup menu is a small window that displays a list of choices. A popup menu can appear at any locaiton that you specified on a component. For example, a popup menu of choices is shown on the window where the user right-clicked the window.

Constructors of the JPopupMenu class:
-JPopupMenu()
creates a popup menu.
-JPopupMenu(String label)
creates a popup menu with the title.

Useful methods of the JPopupMenu class:
-add(Action a):JMenuItem
adds an menu item that its properties and action are specified by the Action object to the popup menu.
-add(String item):JMenuItem
adds a menu item to the popup menu.
-setVisible(boolean visible):void
sets the visibility of popup menu.
-show(Component c,int x,int y):void
displays the popup menu at the specified coordiate (x,y) of the component c.

In the example code below, the MouseListener is registered withe the JFrame window to listen to the mouse pressed event. The JPopupMenu is created with two items-Close and Minimize. The Close menu item is clicked to close the window and the Minimize item will minimize the window.  We determine the text properties and actions of the Close and Minimize menu items by using the AbstactAction classs. This class is extended by the AAction class. The instances of the AAction class are attached to the menu items.
When the user right-clicks the window, the popup menu displays at the clicked point on the window. To detect whether the right button of the mouse is pressed, you need to use the getButton method of the MouseEvent class. This method returns a value that is equivalent to the button of the mouse that is pressed. The left button will be MouseEvent.BUTTON1, the middle button MouseEvent.BUTTON2, and the MouseEvent.BUTTON3 for the right button.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.JFrame;

import javax.swing.JPopupMenu;

class JPopupMenuShow extends JFrame{
JPopupMenu pmenu;
JPopupMenuShow(String title){
setTitle(title);
setSize(500,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setLayout(new BorderLayout());
contpane.addMouseListener(new MouseA());
setVisible(true);
pmenu=new JPopupMenu();
pmenu.add(new AAction("Close"));
pmenu.addSeparator();
pmenu.add(new AAction("Minimize"));

}

class MouseA extends MouseAdapter{
public void mousePressed(MouseEvent e){
if(e.getButton()==MouseEvent.BUTTON3)
pmenu.show(getContentPane(),e.getX(),e.getY());
}
}
class AAction extends AbstractAction{

AAction(String text){
super(text);

}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Close"))
dispose();
else if(e.getActionCommand().equals("Minimize"))
setExtendedState(JFrame.ICONIFIED);
}
}


}

public class JFrameSwing {
public static void main(String[] args){
new JPopupMenuShow("JPopupMenu");
}
}
JPopupMenu Swing

1 comment:

  1. Every weekend i used to pay a quick visit this web site, because i want enjoyment, for the reason that this this
    web page conations really nice funny data too.

    Feel free to visit my blog - 풀싸롱

    ReplyDelete