HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML

"A PopupButton Component"
Vol. 5, Issue 10, p. 82


Listing 1

import java.awt.*;
import java.awt.event.*;

public abstract class PopupButton extends Button implements ActionListener
 private PopupMenu popup;

 protected PopupButton(String[] items, Container parent)

 protected PopupButton(String label, String[] items, Container parent)
  super(label + "  ");

  popup = new PopupMenu();

  MenuItem menuItems[] = new MenuItem[items.length];

  for(int i = 0; i < items.length; i++)
   menuItems[i] = new MenuItem(items[i]);


  addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {

Listing 2

import java.awt.event.*;
import java.awt.*;

public class PopupButtonTest {
 public static void main(String args[]) {
  new PopupButtonTestFrame();

class PopupButtonTestFrame extends Frame {
 Panel panel = new Panel();
 MyPopupButton mpb1 = new MyPopupButton("Colors",new String[]{"Red","Green","Blue"},this);
 MyPopupButton mpb2 = new MyPopupButton("Fruit",new String[]{"Apples","-","Oranges","-","Bannanas"},this);

 PopupButtonTestFrame() {

  /* Add the window listener */
  addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent evt) {
    dispose(); System.exit(0);}});

  /* Size the frame */

  /* Center the frame */
  Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
  Rectangle frameDim = getBounds();
  setLocation((screenDim.width - frameDim.width) / 2,(screenDim.height - frameDim.height) / 2);


  /* Show the frame */

class MyPopupButton extends PopupButton
 public MyPopupButton(String label, String[] items, Container parent)

 public void actionPerformed(ActionEvent evt)
  String command = evt.getActionCommand();

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.