Question

How can I hide all default panels at JColorChooser except HSB?

And is it possible to show just HSB without JTabbedPane, just plain panel

enter image description here

Thank you!

Was it helpful?

Solution

import javax.swing.*;
import javax.swing.colorchooser.*;

class ColorChooserTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JColorChooser cc = new JColorChooser();
                AbstractColorChooserPanel[] panels = cc.getChooserPanels();
                for (AbstractColorChooserPanel accp : panels) {
                    if (accp.getDisplayName().equals("HSB")) {
                        JOptionPane.showMessageDialog(null, accp);
                    }
                }
            }
        });
    }
}

OTHER TIPS

You can try: setChooserPanels method of JColorChooser to do this. More help here.

It can be also done with the simple loop:

AbstractColorChooserPanel[] panels = jColorChooser1.getChooserPanels();
for (AbstractColorChooserPanel accp : panels) {
   if(!accp.getDisplayName().equals("HSB")) {
      jColorChooser1.removeChooserPanel(accp);
   } 
}

If you want to delete panels you can follow this approach Here I'm removing all the other panels except Swatches and RGB,

AbstractColorChooserPanel[] panels=colorChooser.getChooserPanels();
        for(AbstractColorChooserPanel p:panels){
            String displayName=p.getDisplayName();
            switch (displayName) {
                case "HSV":
                    colorChooser.removeChooserPanel(p);
                    break;
                case "HSL":
                    colorChooser.removeChooserPanel(p);
                    break;
                case "CMYK":
                    colorChooser.removeChooserPanel(p);
                    break;
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top