Search code examples
javaswingjcolorchooser

how to increase the size of swatches in JColorChooser?


The size of color palette in JColorChooser/Swatches is very small, also the colors looks similar around a palette.

For my use I need to increase their size as well change the color variation. How to do this?


Solution

  • The size of the "swatches" is controlled by two UI properties, so you can adjust it as needed (default value being 10):

    int length = 20;
    UIManager.put("ColorChooser.swatchesRecentSwatchSize", new Dimension(length, length));
    UIManager.put("ColorChooser.swatchesSwatchSize", new Dimension(length, length));
    

    Customizing the palette itself requires a custom subclass of AbstractColorChooserPanel, mainly a c&p job (because it's package private) of DefaultSwatchChooserPanel in javx.swing.colorchooser. Then replace the default with your custom panel, something along the lines

    JColorChooser chooser = new JColorChooser();
    List<AbstractColorChooserPanel> choosers = 
            new ArrayList<>(Arrays.asList(chooser.getChooserPanels()));
    choosers.remove(0);
    MySwatchChooserPanel swatch = new MySwatchChooserPanel();
    choosers.add(0, swatch);
    chooser.setChooserPanels(choosers.toArray(new AbstractColorChooserPanel[0]));