Adding a Custom Color Chooser Panel to a JColorChooser Dialog : JColorChooser « Swing « Java Tutorial






import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.colorchooser.AbstractColorChooserPanel;

public class Main {
  public static void main(String[] argv) {
    JColorChooser chooser = new JColorChooser();
    chooser.addChooserPanel(new MyChooserPanel());
  }
}

class MyChooserPanel extends AbstractColorChooserPanel {
  public void buildChooser() {
    setLayout(new GridLayout(0, 3));
    makeAddButton("Red", Color.red);
    makeAddButton("Green", Color.green);
    makeAddButton("Blue", Color.blue);
  }

  public void updateChooser() {
  }

  public String getDisplayName() {
    return "MyChooserPanel";
  }

  public Icon getSmallDisplayIcon() {
    return null;
  }
  public Icon getLargeDisplayIcon() {
    return null;
  }
  private void makeAddButton(String name, Color color) {
    JButton button = new JButton(name);
    button.setBackground(color);
    button.setAction(setColorAction);
    add(button);
  }

  Action setColorAction = new AbstractAction() {
    public void actionPerformed(ActionEvent evt) {
      JButton button = (JButton) evt.getSource();

      getColorSelectionModel().setSelectedColor(button.getBackground());
    }
  };
}








14.76.JColorChooser
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