A panel with buttons to pop up three types of color choosers : Color Chooser « Swing JFC « Java






A panel with buttons to pop up three types of color choosers

 
/*
   This program is a part of the companion code for Core Java 8th ed.
   (http://horstmann.com/corejava)

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * @version 1.03 2007-06-12
 * @author Cay Horstmann
 */
public class ColorChooserTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(new Runnable()
         {
            public void run()
            {
               ColorChooserFrame frame = new ColorChooserFrame();
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setVisible(true);
            }
         });
   }
}

/**
 * A frame with a color chooser panel
 */
class ColorChooserFrame extends JFrame
{
   public ColorChooserFrame()
   {
      setTitle("ColorChooserTest");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      // add color chooser panel to frame

      ColorChooserPanel panel = new ColorChooserPanel();
      add(panel);
   }

   public static final int DEFAULT_WIDTH = 300;
   public static final int DEFAULT_HEIGHT = 200;
}

/**
 * A panel with buttons to pop up three types of color choosers
 */
class ColorChooserPanel extends JPanel
{
   public ColorChooserPanel()
   {
      JButton modalButton = new JButton("Modal");
      modalButton.addActionListener(new ModalListener());
      add(modalButton);

      JButton modelessButton = new JButton("Modeless");
      modelessButton.addActionListener(new ModelessListener());
      add(modelessButton);

      JButton immediateButton = new JButton("Immediate");
      immediateButton.addActionListener(new ImmediateListener());
      add(immediateButton);
   }

   /**
    * This listener pops up a modal color chooser
    */
   private class ModalListener implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         Color defaultColor = getBackground();
         Color selected = JColorChooser.showDialog(ColorChooserPanel.this, "Set background",
               defaultColor);
         if (selected != null) setBackground(selected);
      }
   }

   /**
    * This listener pops up a modeless color chooser. The panel color is changed when the user
    * clicks the Ok button.
    */
   private class ModelessListener implements ActionListener
   {
      public ModelessListener()
      {
         chooser = new JColorChooser();
         dialog = JColorChooser.createDialog(ColorChooserPanel.this, "Background Color",
               false /* not modal */, chooser, new ActionListener() // OK
               // button
               // listener
                  {
                     public void actionPerformed(ActionEvent event)
                     {
                        setBackground(chooser.getColor());
                     }
                  }, null /* no Cancel button listener */);
      }

      public void actionPerformed(ActionEvent event)
      {
         chooser.setColor(getBackground());
         dialog.setVisible(true);
      }

      private JDialog dialog;
      private JColorChooser chooser;
   }

   /**
    * This listener pops up a modeless color chooser. The panel color is changed immediately when
    * the user picks a new color.
    */
   private class ImmediateListener implements ActionListener
   {
      public ImmediateListener()
      {
         chooser = new JColorChooser();
         chooser.getSelectionModel().addChangeListener(new ChangeListener()
            {
               public void stateChanged(ChangeEvent event)
               {
                  setBackground(chooser.getColor());
               }
            });

         dialog = new JDialog((Frame) null, false /* not modal */);
         dialog.add(chooser);
         dialog.pack();
      }

      public void actionPerformed(ActionEvent event)
      {
         chooser.setColor(getBackground());
         dialog.setVisible(true);
      }

      private JDialog dialog;
      private JColorChooser chooser;
   }
}

   
  








Related examples in the same category

1.JColorChooser dialog with a custom preview pane.JColorChooser dialog with a custom preview pane.
2.JColorChooser dialog with the custom GrayScalePanel picker tab.JColorChooser dialog with the custom GrayScalePanel picker tab.
3.A quick test of the JColorChooser dialogA quick test of the JColorChooser dialog
4.ColorChooser Sample 1ColorChooser Sample 1
5.Color Chooser DemoColor Chooser Demo
6.System Color ChooserSystem Color Chooser
7.JColorChooser demoJColorChooser demo
8.ColorChooser Demo 2ColorChooser Demo 2
9.Swing ColorChooser DemoSwing ColorChooser Demo
10.Setting the Order of the Color Chooser Panel Tabs in a JColorChooser Dialog
11.Removing a Color Chooser Panel from a JColorChooser Dialog
12.Preview pane simply displays the currently selected color.
13.Retrieving the Color Chooser Panels in a JColorChooser Dialog
14.Customizing the Preview Panel of a JColorChooser Dialog
15.Getting and Setting the Selected Color in a JColorChooser Dialog
16.Creating a JColorChooser Dialog
17.Listening for Changes to the Selected Color in a JColorChooser Dialog
18.Listening for OK and Cancel Events in a JColorChooser Dialog
19.Removing the Preview Panel from a JColorChooser Dialog
20.Adding a Custom Color Chooser Panel to a JColorChooser Dialog
21.extends AbstractColorChooserPanelextends AbstractColorChooserPanel
22.Choose foreground or background color