I want to create a JTabbedPane using the Nimbus Look and Feel that has different colors for each tab. I have found multiple ways to change the colors, but as far as I can tell, these technique apply globally to all tabs with a certain state (such as changing all tabs with TabbedPane:TabbedPaneTab[Enabled].backgroundPainter
to be the same color)
Two useful SO links I have found (close but not quite the answer to my question):
Override Swing Nimbus L&F primary color per component instance
Set the Background Color for JTabbedPane
Below is a mockup image of what I'd like to achieve.
*edit - sorry forgot to include demo program source code. (This doesn't create the coloring but otherwise launches a new window with the appropriate tab pane.):
public class TabbedPaneExample extends JFrame {
public static void main(String args[]) {
try {
for(javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.
getInstalledLookAndFeels()) {
if("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
catch(Exception ex) {
ex.printStackTrace();
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TabbedPaneExample().setVisible(true);
}
});
}
public TabbedPaneExample(){
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
JTabbedPane pane = new JTabbedPane();
pane.addTab("Blue", new JLabel("Blue tab"));
pane.addTab("Red", new JLabel("Red tab"));
pane.addTab("Yellow", new JLabel("Yellow tab"));
this.add(pane);
pack();
}
}
*Edit - In regards to zilk's answer below - it does not appear to work. Extending BasicTabbedPaneUI
causes the Nimbus specific rendering to be lost. In order to keep the Nimbus rendering, SynthTabbedPaneUI
needs to be extended. However, SynthTabbedPaneUI
doesn't call paintTabBackground
, instead it calls paintTabArea
, which is both a protected method and an overloaded private helper method. It looks like all the logic occurs in the private helper method. I can't reproduce that private method in my own code because of private instance variables in SynthTabbedPaneUI
and package private code in SynthContext
which is used by paintTabArea
.