Choose foreground or background color : JColorChooser « Swing « Java Tutorial

 * Created on 17.12.2004

This file is part of BORG.

    BORG 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 2 of the License, or
    (at your option) any later version.

    BORG is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with BORG; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Copyright 2003 by Mike Berger

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;

 * GUI control to easy choose foreground or background color.
 * Indicates color to be stored by its own foreground or background.
 * @author bsv
public class JButtonKnowsBgColor extends JButton {
  // colorProperty is ONE color, but can be indicated by fore or back color
  protected Color colorProperty;
  // bg=true means "choosed color is background color"
  // bg=false means "choosed color is foreground color"
  protected boolean bg;
  public JButtonKnowsBgColor( String p_text, Color p_color, boolean p_bg ){
    setText( p_text );
    setColorProperty( p_color );
    setBg( p_bg );
    addActionListener(new ModalListener());
  public void setColorByProperty(){
    if( isBg() ){
      setBackground( getColorProperty() );
    } else {
      setForeground( getColorProperty() );

  // for testing purposes only
  public static void main(String[] args) {
    JButtonKnowsBgColor jbkbc = new JButtonKnowsBgColor( "choose back", Color.RED, true );
    JButtonKnowsBgColor jbkbc1 = new JButtonKnowsBgColor( "choose fore", Color.BLUE, false );
    JFrame jf = new JFrame();
    jf.setLayout( new BorderLayout() );
    jf.getContentPane().add( jbkbc, BorderLayout.NORTH );
    jf.getContentPane().add( jbkbc1, BorderLayout.CENTER );
    jf.setSize( 100, 200 );
   * @return Returns the color.
  public Color getColorProperty() {
    return colorProperty;
   * @param color The color to set.
  public void setColorProperty(Color color) {
    this.colorProperty = color;
   * @return Returns the bg.
  protected boolean isBg() {
    return bg;
   * @param bg The bg to set.
  protected void setBg(boolean bg) { = bg;

  private class ModalListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
      Color selected = JColorChooser.showDialog(
        isBg()?"Set background":"Set foreground", 


14.76.1.Display Color chooser dialogDisplay Color chooser dialog
14.76.2.Use a Color Chooser
14.76.3.Creating a JColorChooser Dialog
14.76.4.Getting and Setting the Selected Color in a JColorChooser Dialog
14.76.5.Preview pane simply displays the currently selected color.
14.76.6.Listening to Color Selection ChangesListening to Color Selection Changes
14.76.7.Creating and Showing a JColorChooser Pop-Up WindowCreating and Showing a JColorChooser Pop-Up Window
14.76.8.Customizing Action Listeners on JColorChooser ButtonsCustomizing Action Listeners on JColorChooser Buttons
14.76.9.Linking JColorChooser with component's colorLinking JColorChooser with component's color
14.76.10.Dragging-and-Dropping Colors Across JColorChooser ComponentsDragging-and-Dropping Colors Across JColorChooser Components
14.76.11.JColorChooser with custom preview panel
14.76.12.Changing the Color Chooser PanelsChanging the Color Chooser Panels
14.76.13.Setting the Order of the Color Chooser Panel Tabs in a JColorChooser Dialog
14.76.14.Retrieving the Color Chooser Panels in a JColorChooser Dialog
14.76.15.Removing a Color Chooser Panel from a JColorChooser Dialog
14.76.16.Removing the Preview Panel from a JColorChooser Dialog
14.76.17.Customizing the Preview Panel of a JColorChooser Dialog
14.76.18.Listening for OK and Cancel Events in a JColorChooser Dialog
14.76.19.Adding a Custom Color Chooser Panel to a JColorChooser Dialog
14.76.20.Customizing a JColorChooser Look and Feel
14.76.21.Choose foreground or background color