Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...

Swing Chapter 14. (The basics) Dialogs. Easy for reading, Click here!

Custom Search
Swing Chapter 14. (The basics) Dialogs. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 4/5 

Previous Page Previous Page (3/5) - Next Page (5/5) Next Page
Subpages: 1. Dialogs and choosers overview 
Adding an "About" dialog 
JOptionPane message dialogs 
4. Customizing JColorChooser 
5. Customizing JFileChooser 

14.4  Customizing JColorChooser

In chapter 12 we developed a custom menu item allowing quick and easy selection of a color for the background and foreground of a JTextArea. In section 14.1 we built off of this example to add a simple "About" dialog. In this section we'll build off of it further, and construct a customized JColorChooser allowing a much wider range of color selection. Our implementation includes a preview component, PreviewPanel, that illustrates how text will appear with chosen background and foreground colors. Note that we have to return both background and foreground selection values when the user dismisses the color chooser in order to update the text component properly.

Figure 14.12 JColorChooser with custom PreviewPanel component capable of returning two Color selections.

<<file figure14-12.gif>>

UI Guideline : Preview Improves Usability

In this example, the User may have a goal of "Select suitable colours for a banner headline." By allowing the User to view a WYSIWYG preview, usability is improved. The user doesn't have to experiment with selection, which involves opening and closing the dialog several times. Instead, she can achieve the goal on a single visit to the color chooser dialog.

The Code: BasicTextEditor.java

see \Chapter14\3

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class BasicTextEditor extends JFrame


  // Unchanged code from section 14.2

  protected JColorChooser m_colorChooser;

  protected PreviewPanel m_previewPanel;

  protected JDialog m_colorDialog;

  public BasicTextEditor() {

    super("BasicTextEditor with JColorChooser");

    setSize(450, 350);

    ImageIcon icon = new ImageIcon("smallIcon.gif");


    m_colorChooser = new JColorChooser();

    m_previewPanel = new PreviewPanel(m_colorChooser);


    // Unchanged code from section 14.2


  protected JMenuBar createMenuBar() {

    // Unchanged code from section 14.2

    Action actionChooser = new AbstractAction("Color Chooser") {

      public void actionPerformed(ActionEvent e) {


        if (m_colorDialog == null)

        m_colorDialog = JColorChooser.createDialog(


          "Select Background and Foreground Color",

          true, m_colorChooser, m_previewPanel, null);






        if (m_previewPanel.isSelected()) {









    item =  mOpt.add(actionChooser); 



    // Unchanged code from section 14.2



// Unchanged code from section 14.2

class PreviewPanel extends JPanel

 implements ChangeListener, ActionListener


  protected JColorChooser m_chooser;

  protected JLabel m_preview;

  protected JToggleButton m_btBack;

  protected JToggleButton m_btFore;

  protected boolean m_isSelected = false;

  public PreviewPanel(JColorChooser chooser) {

    this(chooser, Color.white, Color.black);


  public PreviewPanel(JColorChooser chooser,

   Color background, Color foreground) {

    m_chooser = chooser;


    setLayout(new BorderLayout());

    JPanel p = new JPanel(new GridLayout(2, 1, 0, 0));

    ButtonGroup group = new ButtonGroup();

    m_btBack = new JToggleButton("Background");





    m_btFore = new JToggleButton("Foreground");




    add(p, BorderLayout.WEST);

    p = new JPanel(new BorderLayout());

    Border b1 = new EmptyBorder(5, 10, 5, 10);

    Border b2 = new BevelBorder(BevelBorder.RAISED);

    Border b3 = new EmptyBorder(2, 2, 2, 2);

    Border cb1 = new CompoundBorder(b1, b2);

    Border cb2 = new CompoundBorder(cb1, b3);


    m_preview = new JLabel("Text colors preview",




    m_preview.setFont(new Font("Arial",Font.BOLD, 24));


    p.add(m_preview, BorderLayout.CENTER);

    add(p, BorderLayout.CENTER);



  protected boolean isSelected() {

    return m_isSelected;


  public void setTextBackground(Color c) {



  public Color getTextBackground() {

    return m_preview.getBackground();


  public void setTextForeground(Color c) {



  public Color getTextForeground() {

    return m_preview.getForeground();


  public void stateChanged(ChangeEvent evt) {

    Color c = m_chooser.getColor();

    if (c != null) {

      if (m_btBack.isSelected())






  public void actionPerformed(ActionEvent evt) {

    if (evt.getSource() == m_btBack)


    else if (evt.getSource() == m_btFore)



      m_isSelected = true;



Understanding the Code

Class BasicTextEditor

New instance variables:

JColorChooser m_colorChooser: stored JColorChooser to avoid unnecessary instantiation.

PreviewPanel m_previewPanel: instance of our custom color previewing component.

JDialog m_colorDialog: stored JDialog acting as the parent of m_colorChooser.

The constructor instantiates m_colorChooser and m_previewPanel, assigning m_previewPanel as m_colorChooser's preview component using the setPreviewPanel() method. 

The menu bar receives a new menu item titled "Color Chooser" set up in the createMenuBar() method as an Action implementation. When selected, this item first repaints our application frame to ensure that the area covered by the popup menu is refreshed properly. Then it checks to see if our m_colorDialog has been instantiated yet. If not we call JColorChooser's static createDialog() method to wrap m_colorChooser in a dialog, and use m_previewPanel as an ActionListener for the "OK" button (see 14.1.3). Note that this instantiation only occurs once.

We then assign the current colors of m_monitor to m_previewPanel (recall that m_monitor is the JTextArea central to this application). The reason we do this is because the foreground and background can also be assigned by our custom menu color choosers. If this occurs m_previewPanel is not notified, so we update the selected colors each time the dialog is invoked.

The dialog is then shown and the main application thread waits for it to be dismissed. When the dialog is dismissed m_previewPanel is checked for whether or not new colors have been selected, using its isSelected() method (see below). If new colors have been chosen they are assigned to m_monitor.

Note: We have purposely avoided updating the selected colors in our custom color menu components. The reason we did this is because in a more professional implementation we would most likely not offer both methods for choosing text component colors. If we did want to support both methods we would need to determine the closest color in our custom color menus that matches the corresponding color selected with JColorChooser (because JColorChooser offers a much wider range of choices).

Class PreviewPanel

This class represents our custom color preview component designed for use with JColorChooser. It extends JPanel and implements two listener interfaces, ChangeListener and ActionListener. It displays selected foreground and background colors in a label, and includes two JToggleButtons used to switch between background color and foreground color selection modes. Instance variables:

JColorChooser m_chooser: a reference to the hosting color chooser.

JLabel m_preview: label to preview background and foreground colors.

JToggleButton m_btBack: toggle button to switch to background color selection.

JToggleButton m_btFore: toggle button to switch to foreground color selection.

boolean m_isSelected: flag indicating a selection has taken place.

The first PreviewPanel constructor takes a JColorChooser as parameter and delegates its work to the second constructor, passing it the JColorChooser as well as white and black Colors for the initial background and foreground colors respectively. As we discussed in the beginning of this chapter, JColorChooser's ColorSelectionModel fires ChangeEvent's when the selected Color changes. So we start by registering this component as a ChangeListener with the given color chooser's model.

A BorderLayout is used to manage this container and two toggle buttons are placed in a 2x1 GridLayout, which is added to the WEST region. Both buttons receive a this reference as an ActionListener. A label with a large font is then placed in the CENTER region. This label is surrounded by a decorative, doubly-compounded border consisting of an EmptyBorder, BevelBorder, and another EmptyBorder. The foreground and background colors of this label are assigned as those values passed to the constructor.

Several methods are used to set and get the selected colors and do not require any special explanation. The stateChanged() method will be called when the color chooser model fires ChangeEvents. Depending on which toggle button is selected, this method updates the background or foreground color of the preview label.

The actionPerformed() method will be called when one of the toggle buttons is pressed. It assignes the stored background or foreground, depending which button is pressed, as the color of the hosting JColorChooser. This method is also called when the "OK" button is pressed, in which case the m_isSelected flag is set to true.

Running the Code

Select the "Color Chooser" menu item to bring up our customized JColorChooser shown in figure 14.12. Select a background and foreground color using any of the available color panes. Verify that the preview label is updated accordingly to reflect the current color selection, and the currently selected toggle button. Press the "OK" button to dismiss the dialog and note that both the selected foreground and background colors are assigned to our application's text area. Also note that pressing the "Cancel" button dismisses the dialog without making any color changes.

[ Return to Swing (Book) ]

Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color

[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3

[ More in News Section ]

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy