A choser



Keywords: java programming, learn java, java sample code, java swing, java ui, java user interface, swing components, swing events, swing layout
Description: This Swing Java Tutorial describes developing graphical user interfaces (GUIs) for applications and applets using Swing components

Use the JColorChooser class to enable users to choose from a palette of colors. A color chooser is a component that you can place anywhere within your program GUI. The JColorChooser API also makes it easy to bring up a dialog (modal or not) that contains a color chooser.

The color chooser consists of everything within the box labeled Choose Text Color. This is what a standard color chooser looks like in the Java Look & Feel. It contains two parts, a tabbed pane and a preview panel. The three tabs in the tabbed pane select chooser panels. The preview panel below the tabbed pane displays the currently selected color.

The JColorChooser constructor in the previous code snippet takes a Color argument, which specifies the chooser's initially selected color. If you do not specify the initial color, then the color chooser displays Color.white. See the Color API documentation for a list of color constants you can use.

A color chooser uses an instance of ColorSelectionModel to contain and manage the current selection. The color selection model fires a change event whenever the user changes the color in the color chooser. The example program registers a change listener with the color selection model so that it can update the banner at the top of the window. The following code registers and implements the change listener:

See How to Write a Change Listener for general information about change listeners and change events.

A basic color chooser, like the one used in the example program, is sufficient for many programs. However, the color chooser API allows you to customize a color chooser by providing it with a preview panel of your own design, by adding your own chooser panels to it, or by removing existing chooser panels from the color chooser. Additionally, the JColorChooser class provides two methods that make it easy to use a color chooser within a dialog.

Now let's turn our attention to ColorChooserDemo2. a modified version of the previous demo program that uses more of the JColorChooser API.

  • Removes the preview panel
  • Removes all of the default chooser panels
  • Adds a custom chooser panel

This program also adds a button that brings up a color chooser in a dialog, which you can use to set the banner background color.

The JColorChooser class provides two class methods to make it easy to use a color chooser in a dialog. ColorChooserDemo2 uses one of these methods, showDialog. to display the background color chooser when the user clicks the Show Color Chooser. button. Here is the single line of code from the example that brings up the background color chooser in a dialog:

The first argument is the parent for the dialog, the second is the dialog title, and the third is the initially selected color.

The dialog disappears under three conditions: the user chooses a color and clicks the OK button, the user cancels the operation with the Cancel button, or the user dismisses the dialog with a frame control. If the user chooses a color, the showDialog method returns the new color. If the user cancels the operation or dismisses the window, the method returns null. Here is the code from the example that updates the banner background color according to the value returned by showDialog :

The dialog created by showDialog is modal. If you want a non-modal dialog, you can use JColorChooser 's createDialog method to create the dialog. This method also lets you specify action listeners for the OK and Cancel buttons in the dialog window. Use JDialog 's show method to display the dialog created by this method. For an example that uses this method, see Specifying Other Editors in the How to Use Tables section.

By default, the color chooser displays a preview panel. ColorChooserDemo2 removes the text color chooser's preview panel with this line of code:

This effectively removes the preview panel because a plain JPanel has no size and no default view. To set the preview panel back to the default, use null as the argument to setPreviewPanel .

To provide a custom preview panel, you also use setPreviewPanel. The component you pass into the method should inherit from JComponent. specify a reasonable size, and provide a customized view of the current color. To get notified when the user changes the color in the color chooser, the preview panel must register as a change listener on the color chooser's color selection model as described previously .

  • Swatches — for choosing a color from a collection of swatches.
  • HSV — for choosing a color using the Hue-Saturation-Value color representation. Prior to JDK 7, this was called HSB, for Hue-Saturation-Brightness.
  • HSL — for choosing a color using the Hue-Saturation-Lightness color representation. This is new in JDK 7.
  • RGB — for choosing a color using the Red-Green-Blue color model.
  • CMYK — for choosing a color using the process color or four color model. This is new in JDK 7.

You can extend the default color chooser by adding chooser panels of your own design with addChooserPanel. or you can limit it by removing chooser panels with removeChooserPanel .

If you want to remove all of the default chooser panels and add one or more of your own, you can do this with a single call to setChooserPanels. ColorChooserDemo2 uses this method to replace the default chooser panels with an instance of CrayonPanel. a custom chooser panel. Here is the call to setChooserPanels from that example:

The code is straighforward: it creates an array containing the CrayonPanel. Next the code calls setChooserPanels to set the contents of the array as the color chooser's chooser panels.

CrayonPanel is a subclass of AbstractColorChooserPanel and overrides the five abstract methods defined in its superclass:

void buildChooser() Creates the GUI that comprises the chooser panel. The example creates four toggle buttons — one for each crayon — and adds them to the chooser panel. void updateChooser() This method is called whenever the chooser panel is displayed. The implementation of this method selects the toggle button that represents the currently selected color.

String getDisplayName() Returns the display name of the chooser panel. The name is used on the tab for the chooser panel. Here is the example getDisplayName method:






Photogallery A choser:


THISGIRLCAN on Pinterest | Strong Women, Feminism and Powerful Women


X Resources and other Chooser Setup


We all chose death for Dzhokhar Tsarnaev - The Boston Globe


LunarUi - Theme Chooser 3.5.4 by R3D-X7 on DeviantArt


I Didn't Choose The Thug Life, The Thug Life Chose Me | Know Your Meme


Lewis Hamilton shows off his bleached hair at GQ Men Of The Year ...


Theme]Moto *hdpi/mdpi* [Theme chooser apk]  | HTC Hero CDMA | XDA ...


Lewis Hamilton shows off his bleached hair at GQ Men Of The Year ...


LunarUi - Theme Chooser 3.5.4 by R3D-X7 on DeviantArt


Charlotte's Zach Jarrett chose a different track from the 'family ...


Caitlyn Jenner Explains Why She Chose Not to Spell Her Name With a "K"


Open Drum: why I chose to have my skin branded - The Drum ...


Theme]Moto *hdpi/mdpi* [Theme chooser apk]  | HTC Hero CDMA | XDA ...


Doc Rivers: DeAndre Jordan chose 'substance and character over ...


Logan Paul does splits in NYC - Tech Insider


Time Versus Debt: Why These Students Chose Community College - All ...


Why Chelsea Handler chose Netflix over TV - Fortune


How EdgeBio chose their LIMS  an interview with Justin Johnson ...


Jose Mourinho's braless daughter Matilde stuns at GQ Men Of The ...


Bibi: Bad news for Israel or Palestine? - Al Jazeera English