Retrieving the Color Chooser Panels in a JColorChooser Dialog - Java Swing

Java examples for Swing:JColorChooser

Description

Retrieving the Color Chooser Panels in a JColorChooser Dialog

Demo Code


import javax.swing.JColorChooser;
import javax.swing.colorchooser.AbstractColorChooserPanel;

public class Main {
  public void m() throws Exception {
    JColorChooser chooser = new JColorChooser();

    // Retrieve the swatch chooser
    findPanel(chooser, "javax.swing.colorchooser.DefaultSwatchChooserPanel");

    // Retrieve the HSB chooser
    findPanel(chooser, "javax.swing.colorchooser.DefaultHSBChooserPanel");

    // Retrieve the RGB chooser
    findPanel(chooser, "javax.swing.colorchooser.DefaultRGBChooserPanel");

  }//from   w w  w .j  a  va  2s . c  o m
  public AbstractColorChooserPanel findPanel(JColorChooser chooser, String name) {
    AbstractColorChooserPanel[] panels = chooser.getChooserPanels();

    for (int i = 0; i < panels.length; i++) {
      String clsName = panels[i].getClass().getName();

      if (clsName.equals(name)) {
        return panels[i];
      }
    }
    return null;
  }
}

Related Tutorials