Java Reference
In-Depth Information
Using JColorChooser
Once you've created a JColorChooser from a constructor, you can place it in any Container , just
like any other Component . For instance, the source shown in Listing 9-7 created the GUI shown
earlier in Figure 9-18.
Listing 9-7. Using a JColorChooser in Your JFrame
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.colorchooser.*;
public class ColorSample {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("JColorChooser Popup");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label = new JLabel("I Love Swing", JLabel.CENTER);
label.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
frame.add(label, BorderLayout.SOUTH);
final JColorChooser colorChooser =
new JColorChooser(label.getBackground());
colorChooser.setBorder(
BorderFactory.createTitledBorder("Pick Foreground Color"));
// More source to come
frame.add(colorChooser, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Although this source code creates the GUI, selecting a different color within the
JColorChooser doesn't do anything yet. Let's now look at the code that causes color changes.
Search WWH ::




Custom Search