Java Tutorials Code Sample –

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.colorchooser.*;
 * requires these files:
 *   images/red.gif
 *   images/yellow.gif
 *   images/green.gif
 *   images/blue.gif
public class ColorChooserDemo2 extends JPanel
                               implements ActionListener,
                                          ChangeListener {
    public JLabel banner;
    public JColorChooser tcc;
    public ColorChooserDemo2() {
        super(new BorderLayout());
        //Set up banner to use as custom preview panel
        banner = new JLabel("Welcome to the Tutorial Zone!",
        banner.setFont(new Font("SansSerif", Font.BOLD, 24));
        banner.setPreferredSize(new Dimension(100, 65));
        JPanel bannerPanel = new JPanel(new BorderLayout());
        bannerPanel.add(banner, BorderLayout.CENTER);
        //Set up color chooser for setting background color
        JPanel panel = new JPanel(); //use FlowLayout
        JButton bcc = new JButton("Show Color Chooser...");
                                "Choose Background Color"));
        //Set up color chooser for setting text color
        tcc = new JColorChooser();
        tcc.setBorder(BorderFactory.createTitledBorder("Choose Text Color"));
        //Remove the preview panel
        tcc.setPreviewPanel(new JPanel());
        //Override the chooser panels with our own
        AbstractColorChooserPanel panels[] = { new CrayonPanel() };
        add(bannerPanel, BorderLayout.PAGE_START);
        add(panel, BorderLayout.CENTER);
        add(tcc, BorderLayout.PAGE_END);
    public void actionPerformed(ActionEvent e) {
        Color newColor = JColorChooser.showDialog(
                                       "Choose Background Color",
        if (newColor != null) {
    public void stateChanged(ChangeEvent e) {
        Color newColor = tcc.getColor();
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("ColorChooserDemo2");
        //Create and set up the content pane.
        JComponent newContentPane = new ColorChooserDemo2();
        newContentPane.setOpaque(true); //content panes must be opaque
        //Display the window.
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s