Skip to content

Instantly share code, notes, and snippets.

@pgtwitter
Last active August 21, 2025 14:10
Show Gist options
  • Save pgtwitter/e0e4fd7d0c5084bc17e6e9c56fae4def to your computer and use it in GitHub Desktop.
Save pgtwitter/e0e4fd7d0c5084bc17e6e9c56fae4def to your computer and use it in GitHub Desktop.

JColorChooser RGB Rounding Issue in Java 21 on macOS

Summary

When using JColorChooser.showDialog with colorTransparencySelectionEnabled=false in Java 21 on macOS, RGB values in the range 0-64 are rounded down by 1 (e.g., 51/51/51/25550/50/50/255), and values in 65-127 show inconsistent rounding (odd values match, even values are decremented by 1). This issue does not occur when colorTransparencySelectionEnabled=true or when using JColorChooser.createDialog with explicit JColorChooser instance management and the sequence of setColorTransparencySelectionEnabled(false) followed by setColor.

This document describes the issue, provides steps to reproduce, and outlines a workaround.

Environment

  • Java: 21.0.7 LTS
  • OS: macOS

Steps to Reproduce

Use JColorChooser.showDialog with colorTransparencySelectionEnabled=false to set a color, such as 51/66/120/255. Look at the RGB panel or just click OK to confirm. You will see that the panel values are different or the standard output values are different from what you expected.

import javax.swing.JColorChooser;
import javax.swing.JFrame;
import java.awt.Color;

public class Main {
    public static void main(String[] args) {
        Color c = new Color(51, 66, 120);
        Color selected = JColorChooser.showDialog(new JFrame(), "Select Color", c, false);
        if (selected != null) {
            System.err.println(selected.getRed() + "/" +
                              selected.getGreen() + "/" +
                              selected.getBlue() + "/" +
                              selected.getAlpha());
        }
    }
}

Expected Output

51/66/120/255

Actual Output

50/64/119/255

When colorTransparencySelectionEnabled=true, no rounding occurs, and all RGB values are returned correctly.

Workaround

Using JColorChooser.createDialog with explicit JColorChooser instance management resolves the issue. The key is to call setColor after setColorTransparencySelectionEnabled(false) for each AbstractColorChooserPanel.

import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import java.awt.Color;

public class Main {
    public static void main(String[] args) {
        Color c = new Color(51, 66, 120);
        JColorChooser colorChooser = new JColorChooser();
        for (AbstractColorChooserPanel p : colorChooser.getChooserPanels()) {
            p.setColorTransparencySelectionEnabled(false);
        }
        colorChooser.setColor(c);
        JDialog d = JColorChooser.createDialog(new JFrame(), "Select Color", true, colorChooser, e -> {
            Color selected = colorChooser.getColor();
            if (selected != null) {
                System.err.println(selected.getRed() + "/" +
                                  selected.getGreen() + "/" +
                                  selected.getBlue() + "/" +
                                  selected.getAlpha());
            }
        }, e -> {
        });
        d.setVisible(true);
    }
}

Output

51/66/120/255 

Why It Works

  • Calling setColor after setColorTransparencySelectionEnabled(false) ensures the color is set directly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment