12.7 Developing a Custom Dialog


While you might rely entirely on the standard color chooser dialog, it is possible to create a color chooser component and use it inside your own dialogs or applications. Let's take a look at a fancy font chooser that lets you pick the face, style, and color. Figure 12-10 shows an example of such a dialog.

Figure 12-10. A custom dialog window, with a JColorChooser as one piece of it
figs/swng2.1210.gif

It looks like a lot is going on in the code that built this dialog window, but it's not really that bad. The first part of the code is devoted to the tedious business of setting up the graphical-interface pieces. Notice that we create a regular JColorChooser object and never call either the showDialog( ) or createDialog( ) methods. You can also see the piece of code required to catch color updates in that section. We attach a ChangeListener to the ColorSelectionModel for the chooser. The event handler for that listener simply calls updatePreviewColor( ) to keep our custom previewer in sync with the color shown in the chooser.

You'll notice that we're storing our font information in a SimpleAttributeSet object. This object is used with the JTextPane class (and you can find out more about it in Chapter 22). For right now, just know that it has some convenient methods for storing text attributes, such as the font name, bold/italic, and size.

Here's the startup code:

// FontChooser.java // A font chooser that allows users to pick a font by name, size, style, and color. // The color selection is provided by a JColorChooser pane. This dialog builds an // AttributeSet suitable for use with JTextPane. // import javax.swing.*; import javax.swing.event.*; import javax.swing.colorchooser.*; import javax.swing.text.*; import java.awt.*; import java.awt.event.*; public class FontChooser extends JDialog implements ActionListener {   JColorChooser colorChooser;   JComboBox fontName;   JCheckBox fontBold, fontItalic;   JTextField fontSize;   JLabel previewLabel;   SimpleAttributeSet attributes;   Font newFont;   Color newColor;   public FontChooser(Frame parent) {     super(parent, "Font Chooser", true);     setSize(450, 450);     attributes = new SimpleAttributeSet( );     // Make sure that if the user cancels, the window does the right thing.     addWindowListener(new WindowAdapter( ) {       public void windowClosing(WindowEvent e) {         closeAndCancel( );       }     });     // Start the long process of setting up our interface.     Container c = getContentPane( );          JPanel fontPanel = new JPanel( );     fontName = new JComboBox(new String[] {"TimesRoman", "Helvetica", "Courier"});     fontName.setSelectedIndex(1);     fontName.addActionListener(this);     fontSize = new JTextField("12", 4);     fontSize.setHorizontalAlignment(SwingConstants.RIGHT);     fontSize.addActionListener(this);     fontBold = new JCheckBox("Bold");     fontBold.setSelected(true);     fontBold.addActionListener(this);     fontItalic = new JCheckBox("Italic");     fontItalic.addActionListener(this);     fontPanel.add(fontName);     fontPanel.add(new JLabel(" Size: "));     fontPanel.add(fontSize);     fontPanel.add(fontBold);     fontPanel.add(fontItalic);     c.add(fontPanel, BorderLayout.NORTH);          // Set up the color chooser panel and attach a change listener so that color     // updates are reflected in our preview label.     colorChooser = new JColorChooser(Color.black);     colorChooser.getSelectionModel( ).addChangeListener(new ChangeListener( ) {       public void stateChanged(ChangeEvent e) {         updatePreviewColor( );       }     });     c.add(colorChooser, BorderLayout.CENTER);     JPanel previewPanel = new JPanel(new BorderLayout( ));     previewLabel = new JLabel("Here's a sample of this font.");     previewLabel.setForeground(colorChooser.getColor( ));     previewPanel.add(previewLabel, BorderLayout.CENTER);     // Add in the OK and Cancel buttons for our dialog box.     JButton okButton = new JButton("Ok");     okButton.addActionListener(new ActionListener( ) {       public void actionPerformed(ActionEvent ae) {         closeAndSave( );       }     });     JButton cancelButton = new JButton("Cancel");     cancelButton.addActionListener(new ActionListener( ) {       public void actionPerformed(ActionEvent ae) {         closeAndCancel( );       }     });     JPanel controlPanel = new JPanel( );     controlPanel.add(okButton);     controlPanel.add(cancelButton);     previewPanel.add(controlPanel, BorderLayout.SOUTH);     // Give the preview label room to grow.     previewPanel.setMinimumSize(new Dimension(100, 100));     previewPanel.setPreferredSize(new Dimension(100, 100));     c.add(previewPanel, BorderLayout.SOUTH);   }

Let's pause and take a look at the next section of code. The actionPerformed( ) method monitors our font choices from the buttons and text field at the top of our dialog. As font attributes change, we keep the AttributeSet object updated and update our display label. (The listener for the color part of our dialog was attached directly to the color chooser in the above code.) The updatePreviewFont( ) and updatePreviewColor( ) methods allow us to change the font and color of the preview label separately. That's a bit more efficient, especially when the user is picking a color with an RGB slider.

  // Something in the font changed, so figure out what and make a   // new font for the preview label.   public void actionPerformed(ActionEvent ae) {     // Check the name of the font.     if (!StyleConstants.getFontFamily(attributes)                        .equals(fontName.getSelectedItem( ))) {       StyleConstants.setFontFamily(attributes, (String)fontName.getSelectedItem( ));     }     // Check the font size (no error checking yet).     if (StyleConstants.getFontSize(attributes) !=                                     Integer.parseInt(fontSize.getText( ))) {       StyleConstants.setFontSize(attributes, Integer.parseInt(fontSize.getText( )));     }     // Check to see if the font should be bold.     if (StyleConstants.isBold(attributes) != fontBold.isSelected( )) {       StyleConstants.setBold(attributes, fontBold.isSelected( ));     }     // Check to see if the font should be italic.     if (StyleConstants.isItalic(attributes) != fontItalic.isSelected( )) {       StyleConstants.setItalic(attributes, fontItalic.isSelected( ));     }     // And update our preview label     updatePreviewFont( );   }   // Get the appropriate font from our attributes object and update   // the preview label.   protected void updatePreviewFont( ) {     String name = StyleConstants.getFontFamily(attributes);     boolean bold = StyleConstants.isBold(attributes);     boolean ital = StyleConstants.isItalic(attributes);     int size = StyleConstants.getFontSize(attributes);     // Bold and italic don't work properly in beta 4.     Font f = new Font(name, (bold ? Font.BOLD : 0) + (ital ? Font.ITALIC : 0), size);     previewLabel.setFont(f);   }   // Get the appropriate color from our chooser and update previewLabel.   protected void updatePreviewColor( ) {     previewLabel.setForeground(colorChooser.getColor( ));     // Manually force the label to repaint.     previewLabel.repaint( );   }

The last segment of code helps us with the shutdown stage for our dialog. The getNewFont( ) and getNewColor( ) methods allow us to retrieve the selected font and color once the dialog is closed. We can also get the complete attribute set using getAttributes( ). The closeAndSave( ) method stores the font and color information from our preview label into newFont and newColor while closeAndCancel( ) puts null into both fields. After showing this dialog, the application using it should check the value of newFont or newColor to determine whether the user accepted a font choice.

  public Font getNewFont( ) { return newFont; }   public Color getNewColor( ) { return newColor; }   public AttributeSet getAttributes( ) { return attributes; }   public void closeAndSave( ) {     // Save font and color information.     newFont = previewLabel.getFont( );     newColor = previewLabel.getForeground( );     // Close the window.     setVisible(false);   }   public void closeAndCancel( ) {     // Erase any font information and then close the window.     newFont = null;     newColor = null;     setVisible(false);   } } 

Here's the application that puts this dialog to use. It's similar to our first color picker. A single button in the application causes the font chooser dialog to be displayed, and whatever font the user picks through the dialog becomes the font for the button. As with that first program, the main work is done here in the actionPerformed( ) method of the button's event handler. Notice how the application checks the new font choice to see if it is null.

// FontPicker.java // A quick test of the FontChooser dialog // import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.colorchooser.*; public class FontPicker extends JFrame {   Color c;   public FontPicker( ) {     super("JColorChooser Test Frame");     setSize(200,100);     final JButton go = new JButton("Show FontChooser");     go.addActionListener(new ActionListener( ) {       final FontChooser chooser = new FontChooser(FontPicker.this);       boolean first = true;       public void actionPerformed(ActionEvent e) {         chooser.setVisible(true);         // If we got a real font choice, then update our go button.         if (chooser.getNewFont( ) != null) {           go.setFont(chooser.getNewFont( ));           go.setForeground(chooser.getNewColor( ));         }       }     });     getContentPane( ).add(go);     setDefaultCloseOperation(EXIT_ON_CLOSE);   }   public static void main(String args[]) {     FontPicker fp = new FontPicker( );     fp.setVisible(true);   } }

As you develop more commercial applications, you may end up writing some of your own choosers. (A DateChooser would be a good start!) Following the API style for the file and color choosers described in this chapter will make it easier to write your own chooser.



Java Swing
Graphic Java 2: Mastering the Jfc, By Geary, 3Rd Edition, Volume 2: Swing
ISBN: 0130796670
EAN: 2147483647
Year: 2001
Pages: 289
Authors: David Geary

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net