// Purpose. JOptionPane, JFileChooser, JColorChooser demo import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JOptionPaneDemo { public static void main( String[] args ) { JFrame f = new JFrame( "JFC Standard Dialogs" ); f.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); f.getContentPane().add( new JLabel( "The parent window", SwingConstants.CENTER )); f.setSize( 300, 200 ); f.setVisible( true ); System.out.println( "Press to continue: " ); Read.aString(); // Topley, p479 JOptionPane.showMessageDialog( f, "Press OK to continue" ); System.out.println( "MessageDialog complete" ); // Topley, p482 JOptionPane.showMessageDialog( f, "An information dialog", "Information Dialog", JOptionPane.INFORMATION_MESSAGE ); System.out.println( "Information Dialog complete" ); JOptionPane.showMessageDialog( f, "A question dialog", "Question Dialog", JOptionPane.QUESTION_MESSAGE ); System.out.println( "Question Dialog complete" ); JOptionPane.showMessageDialog( f, "A warning dialog", "Warning Dialog", JOptionPane.WARNING_MESSAGE ); System.out.println( "Warning Dialog complete" ); JOptionPane.showMessageDialog( f, "An error dialog", "Error Dialog", JOptionPane.ERROR_MESSAGE ); System.out.println( "Error Dialog complete" ); // Topley, p483 JOptionPane.showMessageDialog( f, "Disk is loaded", "Information Dialog", JOptionPane.INFORMATION_MESSAGE, new ImageIcon( "images/disk.gif" ) ); System.out.println( "Custom Information Dialog complete" ); // Topley, p485 int response = JOptionPane.showConfirmDialog( f, "Do you want to continue?" ); if (response == JOptionPane.YES_OPTION) System.out.println( "YES selected" ); else if (response == JOptionPane.NO_OPTION) System.out.println( "NO selected" ); else if (response == JOptionPane.CANCEL_OPTION) System.out.println( "CANCEL selected" ); else if (response == JOptionPane.CLOSED_OPTION) System.out.println( "CLOSED selected" ); // Topley, p489, p532 String lastStr = "PlayDead"; response = JOptionPane.showOptionDialog( f, "Please check your spelling", "Option Dialog", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] {"Surrender","Retreat",lastStr}, lastStr ); if (response == 0) System.out.println( "Surrender selected" ); else if (response == 1) System.out.println( "Retreat selected" ); else if (response == 2) System.out.println( "PlayDead selected" ); else if (response == JOptionPane.CLOSED_OPTION) System.out.println( "CLOSED selected" ); // Topley, p490 lastStr = JOptionPane.showInputDialog( f, "Please type something and move on !" ); System.out.println( "input is - " + lastStr ); // Topley, p493, p548 JFileChooser chooser = new JFileChooser(); response = chooser.showDialog( f, args.length == 0 ? null : args[0] ); System.out.println( "JFileChooser return value was - " + response ); System.out.println( "file name is - " + chooser.getSelectedFile().getPath() ); // Topley, p520 Color selectedColor = JColorChooser.showDialog( f, "Choose my color", Color.red ); System.out.println( "selected color is " + selectedColor + "\n" ); System.out.println( "Done --- cancel the parent window" ); } }