Popup Window
Home Up Button Checkbox CheckboxGroup Choice Key List Menu Mouse Popup Window Text

 

A pop-up window is a separate window that can be "popped-up" from the main display window.  Experiment with the applet below to see what it does.  Then proceed to understand the code that generates the applet.

An applet would appear here if your browser supported Java.

The applet implements the "KeyListener" to be able to respond to pressing, releasing, and typing individual keys on the keyboard.  To learn more about key events, see the key event handling reference.

The "init" method creates a new instance of "DialogTest" (a class defined at the bottom of the file).  The "setVisible(true)" method displays this window.  Finally a key listener is added to the main display window so that it can respond to key events.

The "paint" method displays a message on the main display window via the "drawString" method.  The message tells the user that to get rid of the pop-up window, the user should press any key on the keyboard.

The "keyPressed" method responds to any key on the keyboard being pressed by calling the "setVisible(false)" method on the "dialog" window.  This causes the "dialog" window to be hidden from view.  The "keyReleased" and "keyTyped" methods are methods that do nothing.  Since the "KeyListener" interface is being implemented, they are both methods that must be specified, even if they do nothing.

At the bottom of the file is a class called "DialogTest".  The constructor of the "DialogTest" class sets the size of the frame to be 500 pixels wide and 500 pixels high.  The background color is set to green.  Should an instance of this class ever be shown (through the "show" method), you will see a 500 by 500 window with a green background.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;          
public class Example extends Applet implements KeyListener
{
	public void init ()
	{
		dialog = new DialogTest();
		dialog.setVisible(true);          
		addKeyListener(this);
	}          
	public void paint(Graphics g)
	{
		g.drawString("Press any key to hide the other window", 50, 50);
	}          
	public void keyPressed (KeyEvent evt) 
	{
		dialog.setVisible(false);
	}          
	public void keyReleased (KeyEvent evt) {}          
	public void keyTyped (KeyEvent evt) {}          
	private DialogTest dialog;          
}          
class DialogTest extends Frame
{
	public DialogTest ()
	{
		setSize(500,500);          
		setBackground(Color.green);          
	}
}