RFR: 4337898: Serializing DefaultTableCellRenderer changes colors
Alexander Zvegintsev
azvegint at openjdk.org
Fri Nov 28 07:49:48 UTC 2025
On Fri, 28 Nov 2025 06:29:45 GMT, Prasanta Sadhukhan <psadhukhan at openjdk.org> wrote:
> When a `JTable `using any objects of type `DefaultTableCellRenderer`, or subclasses, is serialized,
> the colors used to render cells in the JTable subsequent to the call to `writeObject()`
> are forced to the default colors for `DefaultTableCellRenderer`'s immediate base class, JLabel, causing the colors
> defined in the JTable (typically black on white) to be ignored.
>
> The problem seems to stem from a call to
> `installUI `in the `writeObject()` method of `JLabel`, `DefaultTableCellRenderer`'s base class.
> This causes the `setForeground` and `setBackground` methods to be invoked with specific colors, which turn out to be JLabel's defaults.
> Invoking these methods subsequently with parameters of null restores normal operation same as is explicitly done in `DefaultTableCellRenderer.updateUI()`
> https://github.com/openjdk/jdk/blob/195b36f90b789b64f4a0fc867c620935d609a455/src/java.desktop/share/classes/javax/swing/table/DefaultTableCellRenderer.java#L159-L162
>
> CI run is ok..
Did you verify that colors are successfully restored after deserialization? (standard and manually set)
test/jdk/javax/swing/DefaultTableCellRenderer/DefRendererSerialize.java line 46:
> 44: static final String INSTRUCTIONS = """
> 45: A JTable is shown
> 46: If the table text is black on white and not black on gray,
We can set the table's background color to a bright color, such as red, to distinguish it from the gray. On some displays, it can be difficult to see the difference between `java.awt.Color[r=238,g=238,b=238]` and `java.awt.Color[r=255,g=255,b=255]` with the naked eye.
test/jdk/javax/swing/DefaultTableCellRenderer/DefRendererSerialize.java line 62:
> 60: static JFrame createTestUI() {
> 61: String[][] rowData = { {"1-1","1-2","1-3"},
> 62: {"2-1","2-2","2-3"},
I guess the test could be automated.
e.g. if we leave the central cell blank:
Suggestion:
{"2-1","","2-3"},
We can safely retrieve the pixel color from the center of the cell and check it against the white color
Rectangle tableRect = table.getCellRect(1, 1, true);
Point tableOnScreen = table.getLocationOnScreen();
Point p = new Point(
tableOnScreen.x + tableRect.x + tableRect.width / 2,
tableOnScreen.y + tableRect.y + tableRect.height / 2
);
Color pixelColor = robot.getPixelColor(p.x, p.y);
// ... color check
-------------
PR Review: https://git.openjdk.org/jdk/pull/28549#pullrequestreview-3517761910
PR Review Comment: https://git.openjdk.org/jdk/pull/28549#discussion_r2570711017
PR Review Comment: https://git.openjdk.org/jdk/pull/28549#discussion_r2570685639
More information about the client-libs-dev
mailing list