Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 48: How to create a reusable MessageBox class

Display a message on your browser screen, with Java 1.1 or later

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 3

box.askYesNo("Is Java now the defacto 3GL for smart developers?");


and:

box.askOkay("James Gosling come here I need you.");


Additional requirements are:

  • The dialog should not deadlock the thread that called it (see below for a section on what deadlocking is)

  • The window should close itself when a button is clicked

  • The dialog should center itself on the screen for easy reading

  • The dialog should be modal, whether or not a Frame is provided. By modal, we mean that users can only click in the MessageBox window, nowhere else in the application


Finally, what you've been waiting for: MessageBox code

Now that we have our requirements down, we can reveal the fabulous MessageBox.

Examine the source code for MessageBox in a separate window. As this code listing is too long to include in this tip, we will examine only the code highlights. MessageBox uses another reusable class: ImageCanvas. Note the class declaration:

public class MessageBox implements Runnable,
    ActionListener, WindowListener, KeyListener {
and the most important method:
public void ask(String message) {
    if (frame == null) {
        frame = new Frame();
        frameNotProvided = true;
    } else {
        frameNotProvided = false;
    }
    dialog = new Dialog(frame, true); // Modal
    dialog.addWindowListener(this);
    dialog.addKeyListener(this);
    dialog.setTitle(title);
    dialog.setLayout(new BorderLayout(5, 5));
    Panel messagePanel = createMultiLinePanel(message);
    if (imageCanvas == null) {
        dialog.add("Center", messagePanel);
    } else {
        Panel centerPanel = new Panel();
        centerPanel.add(imageCanvas);
        centerPanel.add(messagePanel);
        dialog.add("Center", centerPanel);
    }
    dialog.add("South", buttonPanel);
    dialog.pack();
    enforceMinimumSize(dialog, 200, 100);
    centerWindow(dialog);
    Toolkit.getDefaultToolkit().beep();
    // Start a new thread to show the dialog
    Thread thread = new Thread(this);
    thread.start();
}


We implement the listeners so as to receive these events, and implement Runnable so we can create a fine and dandy Java thread. Let's study the related methods:

public void run() {
    dialog.setVisible(true);
}


It couldn't get much simpler, could it? Notice in ask(), we start a new thread that causes run() to be called, and this shows the dialog. This is how we avoid deadlock, which we'll now pause for a few Web seconds to discuss.

Deadlock: A definition

All Java code runs in a thread or in threads. When starting a Java program by calling a main(), for example, the Java runtime creates a thread and calls main() within that thread. Typically, the main() method will instantiate an entry-point class, which will initialize the system and present a Frame or Dialog to the user. The initial thread dies when the main() method has finished running. The reason the Java runtime itself doesn't end is because the AWT has spawned one or more user threads to manage AWT behavior, including user input via buttons and such.

When the user clicks a button, the underlying "AWT thread" dispatches an ActionEvent to the button's ActionListeners that have the method actionPerformed(ActionEvent evt). Now, suppose in actionPerformed(), you decide to open a modal dialog box to ask the user something. When the modal dialog box is shown on the screen, the code blocks. ("Blocks" means a thread is waiting for notification to proceed, which, in the case of a modal dialog box, will not happen until the window is closed.) This means that the AWT thread that invoked actionPerformed() is waiting for the method to return. That AWT thread now is unavailable to process user input, such as on the dialog box we just opened -- so your application is deadlocked. Shucks.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (1)
Login
Forgot your account info?

cvzxvzBy Anonymous on September 9, 2009, 12:58 pmvzxcvczx

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources