Search code examples
javaswingjcomponent

Nesting JComponents, setForeground has no effect


I'm maintaining a legacy Swing application on a Java 8 platform and I have a hierarchy of JComponents. Specifically, I have a number of JLabel components in a JPanel, organized by a GridLayout. The panel is a cell in a JTable.

Some of my JLabels have had their foreground colors set to red, yet they still render black in the window.

I construct the JPanel, constructing the GridLayout in the constructor parameter list. I'm parsing input and increasing the size of the grid as I go as the size of the grid is dependent on the input and I can't compute it beforehand. The panel does not yet have a parent.

In the debugger the foreground color keeps getting set, seemingly at nearly every step. I presume it's being done by the parent panel.

How can I set my text color and not be overridden?


Solution

  • I discovered it was an id10t error between the chair and the keyboard. A condition was always true, replacing the red JLabel with a black one.