Tuesday, January 18, 2011

JTabbedPane woes on XP LnF

I have had a lot of issues with JPanels within JTabbedPanes. The tabs and added panel body were always being displayed with a grey background, and any inner panels added to the main one would then display with the standard beige colouring, leaving me with ugly grey swathes decorated with brown rectangles. I'm not sure if it was in any way related to working within Netbeans Platform, given that I haven't tried it outside of it, but I do doubt it as the components afflicted are all standard Swing ones.

Solving this the simple but tiresome way necessitated making all of those inner panels translucent so they would display the grey background, a solution that was not ideal as the grey contrasted with the beige in the rest of the interface standing out like the proverbial.

So today I happened to stumble on a new solution whilst looking through the various bug reports on the Sun/Oracle/Java websites (which are all closed as will not fix claiming the issue is not an issue at all, but expected behaviour). All one needs to do is set the UI delegate for the JTabbedPane to BasicTabbedPaneUI or a subclass thereof and the problem is solved. Tabs and the background colour of the panels within them are now displaying as uniformly beige on the XP Look and Feel.

Just make a call like so after creating your JTabbedPane:

myTabbedPane.setUI(new BasicTabbedPaneUI());

and you should be in consistent colouring heaven!

Javadoc References (v6): JTabbedPane  BasicTabbedPaneUI