Setting the Order of the Color Chooser Panel Tabs in a JColorChooser Dialog - Java Swing

Java examples for Swing:JColorChooser

Description

Setting the Order of the Color Chooser Panel Tabs in a JColorChooser Dialog

Demo Code

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

public class Main {

  public void main(String[] argv) {
    JColorChooser chooser = new JColorChooser();

    // Retrieve the number of panels
    int numPanels = chooser.getChooserPanels().length;

    // Create an array with the desired order of panels
    // findPanel() is defined in
    // Retrieving the Color Chooser Panels in a JColorChooser Dialog.
    AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[numPanels];
    newPanels[0] = findPanel(chooser,// w  w w. j  a va  2  s .c  o  m
        "javax.swing.colorchooser.DefaultHSBChooserPanel");
    newPanels[1] = findPanel(chooser,
        "javax.swing.colorchooser.DefaultRGBChooserPanel");
    newPanels[2] = findPanel(chooser,
        "javax.swing.colorchooser.DefaultSwatchChooserPanel");

    // Set the new order of panels
    chooser.setChooserPanels(newPanels);
  }
  // Returns the panel instance with the specified name.
  // Returns null if not found.
  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