Skip to Main Content
Java Swing, 2nd Edition
book

Java Swing, 2nd Edition

by Dave Wood, Robert Eckstein, Marc Loy, James Elliott, Brian Cole
November 2002
Intermediate to advanced content levelIntermediate to advanced
1278 pages
38h 26m
English
O'Reilly Media, Inc.
Content preview from Java Swing, 2nd Edition

The JOptionPane Class

JOptionPane is a utility class used to create complex JDialogs and JInternalFrames (the latter of which is used for lightweight dialogs). Figure 10-2 shows where JOptionPane fits into the class hierarchy; Figure 10-3 shows JOptionPane in four L&Fs. It provides a range of convenient ways to create common pop-up modal dialog boxes, which significantly reduces the amount of code you are required to write, at the expense of forcing the user to drop whatever she’s doing and react to the pop up.

JOptionPane class diagram

Figure 10-2. JOptionPane class diagram

JOptionPanes (showing internal confirm dialogs) in four L&Fs

Figure 10-3. JOptionPanes (showing internal confirm dialogs) in four L&Fs

For example, to create a very simple dialog window with the text “Click OK after you read this” and an OK button without JOptionPane, you’d have to write something like this:

 public void showSimpleDialog(JFrame f) { final JDialog d = new JDialog(f, "Click OK", true); d.setSize(200, 150); JLabel l = new JLabel("Click OK after you read this", JLabel.CENTER); d.getContentPane( ).setLayout(new BorderLayout( )); d.getContentPane( ).add(l, BorderLayout.CENTER); JButton b = new JButton("OK"); b.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent ev) { d.setVisible(false); d.dispose( ); } }); JPanel p = new JPanel( ); // Flow layout will center ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Threads, 3rd Edition

Java Threads, 3rd Edition

Scott Oaks, Henry Wong

Publisher Resources

ISBN: 0596004087Errata PageSupplemental Content