Designing a Simple Nimbus Visualization Box

I have a simple-ish cell rendering that consists of several JLabel (rendering itself extends JPanel ) and I'm trying to make it look visually visually in Nimbus. Basically, what happens is that in the lighter rows (since Nimbus has an alternative row color), my particular cell renderer uses the table background color (which is much darker than the lighter and darker the row colors). In my renderer, I do:

 if (isSelected) { setBackground(table.getSelectionBackground); } else { setBackground(table.getBackground); } 

If I comment on this whole block of code, then all my lines are in the dark color of the line (and not in the background of the table, but not in alternative colors). I'm not sure I even understand what could be! How does the code snippet above create cells with different background colors? Does the color of table.getBackground between calls to my method?

I tried using this piece of code:

 Color alternateColor = sun.swing.DefaultLookup.getColor( peer, peer.getUI, "Table.alternateRowColor"); if (alternateColor != null && row % 2 == 0) setBackground(alternateColor); 

What is in the DefaultTableCellRenderer class. And that seems to have no effect. Has anyone received custom cell handlers that work with a halo?

EDIT . If anyone is interested, this turned out to be a problem with Scala's table renderers, as I actually used Scala, not Java. The answer below works great in a Java program. Separately asked question here .

+4
source share
1 answer

Your first piece of code, if it is beautiful. I think you should use UIManager.getColor("Table.alternateRowColor") for alternate rows and table.getBackground () otherwise. For the selected row, use table.getSelectionBackground (). So your code might look like

 if (isSelected) { setBackground(table.getSelectionBackground()); } else { if ( row % 2 == 0 ) { setBackground(UIManager.getColor("Table.alternateRowColor")); } else { setBackground(table.getBackground()); } } 

Remember to make sure your panel is opaque and the labels are transparent.

Here is a good link to the default Nimbus UI settings: http://www.duncanjauncey.com/java/ui/uimanager/UIDefaults_Java1.6.0_11_Windows_2000_5.0_Nimbus.html

+4
source

All Articles