Learning Java Chapter 14: Using Swing Components
The PopupMenu Class
One of Swing's nifty components is JPopupMenu, a menu that
automatically appears when you press the appropriate mouse button inside of
a component. (On a Windows system, for example, clicking the right mouse
button invokes a popup menu.) Which button you press depends on the
platform you're using; fortunately, you don't have to care--Swing figures
it out for you.
The care and feeding of JPopupMenu is basically the same as
any other menu. You use a different constructor (JPopupMenu(
)) to create it, but otherwise, you build a menu and add elements to
it the same way. The big difference is you don't need to attach it to a
JMenuBar. Instead, just pop up the menu whenever you need it.
The following example, PopupColorMenu, contains three buttons.
You can use a JPopupMenu to set the color of each button or
the frame itself, depending on where you press the mouse. Figure 14-5 shows
the example in action; the user is preparing to change the color of the
bottom button.
Figure 14-5. The PopupColorMenu application
|
//file: PopUpColorMenu.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PopUpColorMenu extends JFrame
implements ActionListener {
JPopupMenu colorMenu;
Component selectedComponent;
public PopUpColorMenu( ) {
super("PopUpColorMenu v1.0");
setSize(100, 200);
setLocation(200, 200);
addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) { System.exit(0); }
});
MouseListener mouseListener = new MouseAdapter( ) {
public void mousePressed(MouseEvent e) { checkPopup(e); }
public void mouseClicked(MouseEvent e) { checkPopup(e); }
public void mouseReleased(MouseEvent e) { checkPopup(e); }
private void checkPopup(MouseEvent e) {
if (e.isPopupTrigger( )) {
selectedComponent = e.getComponent( );
colorMenu.show(e.getComponent(), e.getX(), e.getY( ));
}
}
};
final Container content = getContentPane( );
content.setLayout(new FlowLayout( ));
JButton button = new JButton("Uno");
button.addMouseListener(mouseListener);
content.add(button);
button = new JButton("Due");
button.addMouseListener(mouseListener);
content.add(button);
button = new JButton("Tre");
button.addMouseListener(mouseListener);
content.add(button);
colorMenu = new JPopupMenu("Color");
colorMenu.add(makeMenuItem("Red"));
colorMenu.add(makeMenuItem("Green"));
colorMenu.add(makeMenuItem("Blue"));
getContentPane( ).addMouseListener(mouseListener);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String color = e.getActionCommand( );
if (color.equals("Red"))
selectedComponent.setBackground(Color.red);
else if (color.equals("Green"))
selectedComponent.setBackground(Color.green);
else if (color.equals("Blue"))
selectedComponent.setBackground(Color.blue);
}
private JMenuItem makeMenuItem(String label) {
JMenuItem item = new JMenuItem(label);
item.addActionListener( this );
return item;
}
public static void main(String[] args) {
new PopUpColorMenu( );
}
}
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|