Dialogs: When Later Just Won’t Do

Problem

You need a bit of feedback from the user right now.

Solution

Use a JOptionPane method to show a prebuilt dialog.

Discussion

It’s fairly common to want to confirm an action with the user or to bring some problem to their attention right away, rather than waiting for them to read a logfile that they might or might not get around to. These pop-up windows are called Dialogs. The JOptionPane class has a number of show...Dialog( ) methods that let you display most prebuilt dialogs, including those shown in Figure 13-6.

JOptionPane in action

Figure 13-6. JOptionPane in action

The simplest form is showMessageDialog( ), and its first argument is the owning Frame or JFrame. If you don’t know it, pass null, but Java doesn’t guarantee to give input focus back to your main window when the dialog is dismissed. The second argument is the message text, and the third is the title bar title. Last but not least is code telling which of several prebuilt bitmaps should be displayed. This program produces the “Coded Message” dialog in the figure:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Demonstrate JOptionPane
 */
public class JOptionDemo extends JFrame {

    // Constructor
    JOptionDemo(String s) {
        super(s);

        Container cp = getContentPane(  );
        cp.setLayout(new FlowLayout(  ));

        JButton b = new JButton("Give me a message");
        b.addActionListener(new ActionListener(  ) {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(
                    JOptionDemo.this,
                    "This is your message: etaoin shrdlu", "Coded Message",
                    JOptionPane.INFORMATION_MESSAGE);
            }
        });
        cp.add(b);

        b = new JButton("Goodbye!");
        b.addActionListener(new ActionListener(  ) {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        cp.add(b);

        // the main window
        setSize(200, 150);
        pack(  );
    }

    public static void main(String[] arg) {
        JOptionDemo x = new JOptionDemo("Testing 1 2 3...");
        x.setVisible(true);
    }
}

There are several other ways of using the JOptionPane class. For example, you can call its showDialog() method with a list of strings; each will be displayed on a push button in the dialog. This method blocks until the user selects one of the buttons; the return value of the method is an int telling which button the user clicked on (it returns the array index of the string whose button was pressed). There is also showInputDialog( ) , which lets you prompt the user for a data value. Very, very convenient!

See Also

JDialog lets you write arbitrary complicated dialogs. You subclass them in a manner similar to JFrame, specifying whether you want an application-modal or nonmodal dialog (a modal dialog locks out the rest of the application, which is less convenient for the user but much easier for the programmer). See the Java Swing book for information on JDialog.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.144.194