// A popup menu is implemented by the 'Choice' class in the
// Java AWT. The following code creates a 'Choice' displaying
// the months and sets the intitially displayed month to the
// current month. When you select a month, it also "pops up"
// a second Choice, listing two days of the week. When a day
// is selected, the new Choice disappears.
//
import java.applet.Applet;
import java.awt.*;
import java.util.Date;
class TheirChoice extends Choice
{
MyChoice mc;
public TheirChoice(MyChoice c)
{
mc = c;
addItem("Monday");
addItem("Tuesday");
}
public boolean handleEvent(Event e)
{
if (e.id == Event.ACTION_EVENT)
{
// destroy this choice
System.out.println("Their choice selected");
mc.tc = null;
getParent().remove(this);
return true;
}
return super.handleEvent(e);
}
}
class MyChoice extends Choice
{
TheirChoice tc;
public boolean handleEvent(Event e)
{
if (e.id == Event.ACTION_EVENT)
{
// a choice item selected
// Display another choice
tc = new TheirChoice(this);
getParent().add(tc);
getParent().layout();
return true;
}
return super.handleEvent(e);
}
}
public class Popup extends Applet {
public Popup() {
setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
Label l = new Label("Month: ", Label.RIGHT);
add(l);
// Create the Choice, add items and select() the
// default item to display.
MyChoice c = new MyChoice();
add(c);
c.addItem("January");
c.addItem("February");
c.addItem("March");
c.addItem("April");
c.addItem("May");
c.addItem("June");
c.addItem("July");
c.addItem("August");
c.addItem("September");
c.addItem("October");
c.addItem("November");
c.addItem("December");
Date date = new Date();
// Display the current month
//
c.select(date.getMonth());
show();
}
public void paint(Graphics g) {
Rectangle r = bounds();
g.setColor(getBackground());
g.draw3DRect(r.x + 2, r.y + 2, r.width - 4, r.height - 4, true);
}
public static void main(String args[])
{
// Create a Frame
MainFrame f = new MainFrame("Popup");
// Instantiate the Applet
Popup popup = new Popup();
// Init and start the Applet
popup.init();
popup.start();
// Add the Applet to the Frame
f.add("West", popup);
// Resize and show the Frame
f.resize(300, 300);
f.show();
}
}
class MainFrame extends Frame
{
public MainFrame(String s) {
super(s);
}
// Handle close events by simply exiting
public boolean handleEvent(Event e) {
if (e.id == Event.WINDOW_DESTROY) {
System.exit(0);
}
return false;
}
}