I'm using a JTabbedPane in a project of mine, and I am trying to change the look and feel of the application. When I set the windows look and feel (and any other look and feel for that matter), all the buttons, check buttons etc change to what they should look like, but the tabs still look like the default swing tabs. Here's an example of what it looks like:
The buttons and checkbox look like windows components, but the tabs look like the default swing. If it helps, I'm using a subclass of JTabbedPane that is defined in a separate class file. My question is how can I get the tabs in the JTabbedPane to look like windows tabs?
This code emulates the problem for me. The tabs show up as swing but the buttons show up as a different l&f:
import javax.swing.*;
import java.awt.*;
public class TabbedFrame
{
private static JFrame window;
private static JPanel pane;
private static JTabbedPane tabs = new JTabbedPane();
public static void main(String[] vars)
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
} catch (Exception ee){
ee.printStackTrace();
}
window = new JFrame("tabs");
pane = new JPanel();
window.setSize(200, 300);
window.setLocation(100, 100);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(pane);
Panel[] panes;
panes = new Panel[2];
panes[0] = new Panel();
panes[1] = new Panel();
panes[0].add(new Label("PANEL 1..................."));
panes[1].add(new Label("PANEL 2 ....."));
tabs.addTab("tab 1", panes[0]);
tabs.addTab("tab 2", panes[1]);
tabs.setSize(480, 640);
pane.add(tabs);
pane.add(new JButton("test"));
pane.add(new JCheckBox());
pane.add(new JRadioButton());
tabs.setBounds(20, 20, 200, 200);
window.setVisible(true);
}
}
The JTabbedPane
is created before the PLAF is set. There are at least two fixes.
main
, and put it after the call to set the PLAF.SwingUtilities.updateComponentTreeUI(topLevelContainer)
as shown in the Nested Layout Example. That example allows the PLAF to be changed by the user at run-time.