Standard JOptionPane dialogs are sufficient in most cases. Occasionally, you need to create custom dialogs. In Swing, the JDialog class can be extended to create custom dialogs.
As with JFrame , components are added to the contentPane of JDialog . Creating a custom dialog usually involves laying out user interface components in the dialog, adding buttons for dismissing the dialog, and installing listeners that respond to button actions.
The standard dialog is modal , which means that no other window can be accessed before the dialog is dismissed. However, the custom dialogs derived from JDialog are not modal by default. To make a dialog modal, set its modal property to true . To display an instance of JDialog , set its visible property to true .
Let us create a custom dialog box for choosing colors, as shown in Figure 29.17(a). Use this dialog to choose the color for the foreground of the button, as shown in Figure 29.17(b). When the user clicks the Change Button Text Color button, the Choose Color dialog box is displayed.
Create a custom dialog component named ColorDialog by extending JDialog . Use three sliders to specify red, green, and blue components of a color. The program is given in Listing 29.6.
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 import javax.swing.event.*; 5 6 public class ColorDialog extends JDialog { 7 // Declare color component values and selected color 8 private int redValue, greenValue, blueValue; 9 private Color color = null ; 10 11 // Create sliders 12 private JSlider jslRed = new JSlider( , 128 ); 13 private JSlider jslGreen = new JSlider( , 128 ); 14 private JSlider jslBlue = new JSlider( , 128 ); 15 16 // Create two buttons 17 private JButton jbtOK = new JButton( "OK" ); 18 private JButton jbtCancel = new JButton( "Cancel" ); 19 20 // Create a panel to display the selected color 21 private JPanel jpSelectedColor = new JPanel(); 22 23 public ColorDialog() { 24 this ( null , true ); 25 } 26 27 public ColorDialog(java.awt.Frame parent, boolean modal) { 28 super (parent, modal); 29 setTitle( "Choose Color" ); 30 31 // Group two buttons OK and Cancel 32 JPanel jpButtons = new JPanel(); 33 jpButtons.add(jbtOK); 34 jpButtons.add(jbtCancel); 35 36 // Group labels 37 JPanel jpLabels = new JPanel(); 38 jpLabels.setLayout( new GridLayout( 3 , )); 39 jpLabels.add( new JLabel( "Red" )); 40 jpLabels.add( new JLabel( "Green" )); 41 jpLabels.add( new JLabel( "Blue" )); 42 43 // Group sliders for selecting red, green, and blue colors 44 JPanel jpSliders = new JPanel(); 45 jpSliders.setLayout( new GridLayout( 3 , )); 46 jpSliders.add(jslRed); 47 jpSliders.add(jslGreen); 48 jpSliders.add(jslBlue); 49 50 // Group jpLabels and jpSliders 51 JPanel jpSelectColor = new JPanel(); 52 jpSelectColor.setLayout( new BorderLayout()); 53 jpSelectColor.setBorder( 54 BorderFactory.createTitledBorder( "Select Color" )); 55 jpSelectColor.add(jpLabels, BorderLayout.WEST); 56 jpSelectColor.add(jpSliders, BorderLayout.CENTER); 57 58 // Group jpSelectColor and jpSelectedColor 59 JPanel jpColor = new JPanel(); 60 jpColor.setLayout( new BorderLayout()); 61 jpColor.add(jpSelectColor, BorderLayout.SOUTH); 62 jpColor.add(jpSelectedColor, BorderLayout.CENTER); 63 64 // Place jpButtons and jpColor into the dialog box 65 add(jpButtons, BorderLayout.SOUTH); 66 add(jpColor, BorderLayout.CENTER); 67 pack(); 68 69 jbtOK.addActionListener( new ActionListener() { 70 public void actionPerformed(ActionEvent e) { 71 setVisible( false ); 72 } 73 }); 74 75 jbtCancel.addActionListener( new ActionListener() { 76 public void actionPerformed(ActionEvent e) { 77 color = null ; 78 setVisible( false ); 79 } 80 }); 81 82 jslRed.addChangeListener( new ChangeListener() { 83 public void stateChanged(ChangeEvent e) { 84 redValue = jslRed.getValue(); 85 color = new Color(redValue, greenValue, blueValue); 86 jpSelectedColor.setBackground(color); 87 } 88 }); 89 90 jslGreen.addChangeListener( new ChangeListener() { 91 public void stateChanged(ChangeEvent e) { 92 greenValue = jslGreen.getValue(); 93 color = new Color(redValue, greenValue, blueValue); 94 jpSelectedColor.setBackground(color); 95 } 96 }); 97 98 jslBlue.addChangeListener( new ChangeListener() { 99 public void stateChanged(ChangeEvent e) { 100 blueValue = jslBlue.getValue(); 101 color = new Color(redValue, greenValue, blueValue); 102 jpSelectedColor.setBackground(color); 103 } 104 }); 105 } 106 107 public Dimension getPreferredSize() { 108 return new java.awt.Dimension( 200 , 200 ); 109 } 110 111 /** Return color */ 112 public Color getColor() { 113 return color; 114 } 115 } |
Create a test class to use the color dialog to select the color for the foreground color of the button in Listing 29.7.
1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 5 public class TestColorDialog extends JApplet { 6 private ColorDialog colorDialog1 = new ColorDialog(); 7 private JButton jbtChangeColor = new JButton( "Choose color" ); 8 9 public TestColorDialog() { 10 setLayout( new java.awt.FlowLayout()); 11 jbtChangeColor.setText( "Change Button Text Color" ); 12 jbtChangeColor.addActionListener( new ActionListener() { 13 public void actionPerformed(ActionEvent e) { 14 colorDialog1.setVisible( true ); 15 16 if (colorDialog1.getColor() != null ) 17 jbtChangeColor.setForeground(colorDialog1.getColor()); 18 } 19 }); 20 add(jbtChangeColor); 21 } 22 } |
The custom dialog box allows the user to use the sliders to select colors. The selected color is stored in the color variable. When the user clicks the Cancel button, color becomes null , which implies that no selection has been made.
The dialog box is displayed when the user clicks the "Change Button Text Color" button and is closed when the OK button or the Cancel button is clicked.
Tip
Not setting the dialog modal when needed is a common mistake. In this example, the dialog is set modal in line 24 in ColorDialog.java (Listing 29.6). If the dialog is not modal, all the statements in the "Change Button Text Color" button handler are executed before the color is selected from the dialog box. |