Skip to content
Advertisement

Nimbus takes precedence over manually defined renderer?

Making a GUI in Swing (NetBeans 15, Sun JDK 19.0.1), i’m trying to set custom background color for JTable rows and encountered issues with boolean cells, and i can’t seem to be able to make the background uniform across all cells. Please note that the following code tries to paint the background for the whole table, but my target is to set the background for one line at a time; this code serves the only purpose of highlighting the weird interaction between Nimbus alternate row coloring and the custom renderers i have personally encountered.

The issue seems vastly documented already, here’s what i tried:

First attempt using a custom renderer, like so:

JavaScript

and got this result:
booleans have white background

Uncommenting the c.setOpaque(true); line yields the following: booleans have alternating background

Second attempt, by means of the method PrepareRenderer() as documented here, and i made it this way:

JavaScript

got the exact same behavior as before, down to the detail of uncommenting the setOpaque line.

Further reading revealed that the alternate table row coloring is handled by the Nimbus Look&Feel, which NetBeans IDE configured automagically upon creation of the project, so i tried adding this line after the Nimbus configuration: UIManager.getLookAndFeelDefaults().put("Table.alternateRowColor", Color.GREEN); which led to this
booleans have alternating green background
and to the conclusion that Nimbus might be interfering with the intended cell rendering. Indeed, removing Nimbus altogether gives me the result i want, but sends the rest of the UI back to the middle ages…

Oddly enough, if i select a cell, the whole row gets the correct background, including the boolean cells:
enter image description here

The last relevant piece of info i found is this, in the javadoc for the setBackground() method: It is up to the look and feel to honor this property, some may choose to ignore it. which made me dubious this can even work withoud swapping Nimbus for something else.

My conclusion: no matter where i put the rendering instructions, i can only manage to change the background of the booleans that are not on one of the alternating rows, unless the row is selected.

The question: did i miss some major obvious configuration step? Or perhaps is there a way to disable Nimbus’s Table alternating row colors? Or again, is this some sort of known issue?

(more SO answers: this is not relevant; this does not work;)

Edit: added SSCCE, though being GUI code made from the IDE is far from short.

JavaScript

Advertisement

Answer

You wrote in your question (regarding the code in your question):

the following code tries to paint the background for the whole table

This is what I set out to do. However, you also wrote:

my target is to set the background for one line at a time

Then I tried a different approach – which still paints the entire background red but I’m hoping you will be able to adapt the below code to suit your requirements.

First Approach

As detailed here:
https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/color.html
and here:
https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html

Simply setting the appropriate properties, in class UIManager, achieves [what I believe to be] your desired result, i.e. making the entire JTable background red. This works for all columns apart from the first column of the JTable that displays Boolean values. For that, I created a custom [table] cell renderer, based on the one used by Nimbus look-and-feel, namely:

JavaScript

Here is the code for that renderer:

JavaScript

I also wrote a custom TableModel.

JavaScript

Here is the code for this first approach:

JavaScript

Second Approach

This approach does not modify UIManager defaults but instead overrides method prepareRenderer (in class JTable). Note that this approach also uses the same TableModel and custom renderer that the first approach used.

I overrode method prepareRenderer rather than creating another custom renderer that would be based on the general JTable cell renderer used by Nimbus look-and-feel since that renderer is much more complex than SynthBooleanTableCellRenderer.

Here is the code for the second approach:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement