OK, so it may be just glitz or a passing fad, but with today’s
displays, the 13 original AWT colors are too limiting. Swing’s
JColorChooser
lets you choose from zillions of
colors. From a program’s view, it can be used in three ways:
Construct it and place it in a panel
Call its ConstructDialog( )
and get a
JDialog
back
Call its showDialog( )
and get back the chosen
color
We’ll use the last method, since it’s the simplest and the most likely to be used in a real application. The user has several methods of operating the chooser, too:
The user can pick from one of a few hundred color variants.
This one’s my favorite. The user picks one of Hue, Saturation, or Brightness to be nailed down; by adjusting another by slider, there is a huge range of different pixel values to choose from, by clicking (or, more fun, dragging) in the central area. See Figure 13-9.
The user picks Red, Green, and Blue components by sliders.
Example 13-5 contains a short program that makes it happen.
Example 13-5. JColorDemo.java
import com.darwinsys.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; /* * Colors - demo of Swing JColorChooser. * Swing's JColorChooser can be used in three ways: * <UL><LI>Construct it and place it in a panel; * <LI>Call its ConstructDialog( ) and get a JDialog back * <LI>Call its showDialog( ) and get back the chosen color * </UL> * <P>We use the last method, as it's the simplest, and is how * you'd most likely use it in a real application. * * Originally appeared in the Linux Journal, 1999. */ public class JColorDemo extends JFrame { /** A canvas to display the color in. */ JLabel demo; /** The latest chosen Color */ Color lastChosen; /** Constructor - set up the entire GUI for this program */ public JColorDemo( ) { super("Swing Color Demo"); Container cp = getContentPane( ); JButton jButton; cp.add(BorderLayout.NORTH, jButton = new JButton("Change Color...")); jButton.setToolTipText("Click here to see the Color Chooser"); jButton.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent actionEvent) { Color ch = JColorChooser.showDialog( JColorDemo.this, // parent "Swing Demo Color Popup", // title getBackground( )); // default if (ch != null) demo.setBackground(ch); } }); cp.add(BorderLayout.CENTER, demo = new MyLabel("Your One True Color", 200, 100)); demo.setToolTipText("This is the last color you chose"); pack( ); addWindowListener(new WindowCloser(this, true)); } /** good old main */ public static void main(String[] argv) { new JColorDemo( ).setVisible(true); } }
This program introduces setToolTipText( )
, a
method to set the text for
pop-up
“tooltips” that appear when you position the mouse
pointer over a component and don’t do anything for a given time
(initially half a second). Tooltips originated with Macintosh Balloon
Help, and were refined into ToolTips under Microsoft
Windows.[30] Tooltips are easy to use; the simplest form is shown
here. For more
documentation, see Chapter 3 of the
Java Swing book.
3.137.164.24