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






Adding a Custom Color Chooser Panel to a JColorChooser Dialog

   

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());
    }
  };
}

   
    
    
  








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.extends AbstractColorChooserPanelextends AbstractColorChooserPanel
21.Choose foreground or background color
22.A panel with buttons to pop up three types of color choosers