Main Page

Previous Next

Choosing Custom Colors

We made provision in the status bar for showing a custom color. It would be a shame not to make use of this, so let's add a dialog to enable any color to be chosen. This is going to be a lot easier than you imagine.

To keep it simple, we will implement this as a facility on the general pop-up menu, although in practice you would probably want it accessible from the main menu and the toolbar. We will add a member to the SketchFrame class for the menu item:

private JMenuItem customColorItem;

We just need to add this to the pop-up and to add an action listener for it. This requires two statements in the SketchFrame constructor:

customColorItem = popup.add("Custom Color...");    // Add the item
customColorItem.addActionListener(this);           // and add its listener

You can add these statements following the others that set up the pop-up menu. Selecting this menu item will now cause the actionPerformed()method in the SketchFrame class to be called so we will implement the custom color choice in there. Let's try it out.

Try It Out – Choosing a Custom Color

We will use the facilities provided by the JColorChooser class that does precisely what we want. Here's how we will use it in the actionPerformed() method:

  // Handle About menu action events
  public void actionPerformed(ActionEvent e)  {
    if(e.getSource() == aboutItem) {
      // Create about dialog with the menu item as parent
      JOptionPane.showMessageDialog(this,                          // Parent
                            "Sketcher Copyright Ivor Horton 2001", // Message
                            "About Sketcher",                      // Title
                            JOptionPane.INFORMATION_MESSAGE);      // Message type

    } else if(e.getSource() == fontItem) {      // Set the dialog window position
      Rectangle bounds = getBounds();
      fontDlg.setLocation(bounds.x + bounds.width/3, bounds.y + bounds.height/3);
      fontDlg.setVisible(true);            // Show the dialog

    } else if(e.getSource() == customColorItem) {
      Color color = JColorChooser.showDialog(this, "Select Custom Color",
                                                           elementColor);
      if(color != null) {
        elementColor = color;
        statusBar.setColorPane(color);
      }
    }
  }

With the JColorChooser class imported, recompile and rerun Sketcher and select the Custom Color... menu item from the general pop-up. You will see the dialog below.

Click To expand

How It Works

The JColorChooser class defines a complete color choosing facility that you can use in your own dialog, or create a complete modal dialog by calling the static method showDialog() as we have done here. The arguments to showDialog() are a reference to the parent component for the dialog, the title for the dialog, and the initial color selection. You can choose a color using any of the three tabs that provide different mechanisms for defining the color that you want. When you click on OK, the color that you chose is returned as type Color. Exiting the dialog by any other means than selecting the OK button returns null. We just store the color returned in elementColor, and set it in the status bar pane.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor