In Java 1.5, Sun implemented the Windows XP look and feel for the JTabbedPane. Learn how to modify your JTabbedPane to take advantage of the new Windows XP look and feel.
When creating JTabbedPane, you add a JPanel to each tabbed pane. The JPanel contains the controls in the tab. The following image shows a sample of the JTabbedPane running in Java 1.5 on Windows XP.
You'll notice that the tabs have the XP look and feel, but the content area is a solid color. In Windows XP, the content area of the tab should be a gradient background. In order to get the gradient background to display, you need to modify the JPanel that is added to the JTabbedPane. By default, the JPanel is opaque. You will need to set the JPanel's opaque flag to false so the gradient background will show through.
//Create the panel for a tab JPanel panel1 = new JPanel(new FlowLayout()); panel1.setOpaque(false);
Here's the results of setting the opaque flag on the JPanel.
The background of the tab shows the gradient. For non-rectangular controls such as radio buttons or check boxes, you will need to set the opaque flag to false for each of the radio buttons.
JRadioButton jRadioButton1 = new JRadioButton("Radio 1"); jRadioButton1.setOpaque(false);Finally, the tab and the controls in it have the complete Windows XP look and feel.
The complete code example is below.
package xptabs; import javax.swing.*; import java.awt.*; public class XPTabDemo extends JFrame { public XPTabDemo() { super("Demo"); //Set the default window close operation setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create the JTabbedPane JTabbedPane tabbedPane = new JTabbedPane(); //Create the panel for the tab JPanel panel1 = new JPanel(new FlowLayout()); panel1.setOpaque(false); JRadioButton jRadioButton1 = new JRadioButton("Radio 1"); jRadioButton1.setOpaque(false); JRadioButton jRadioButton2 = new JRadioButton("Radio 2"); jRadioButton2.setOpaque(false); JRadioButton jRadioButton3 = new JRadioButton("Radio 3"); jRadioButton3.setOpaque(false); JRadioButton jRadioButton4 = new JRadioButton("Radio 4"); jRadioButton4.setOpaque(false); //Add the radio buttons to the button group ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(jRadioButton1); buttonGroup.add(jRadioButton2); buttonGroup.add(jRadioButton3); buttonGroup.add(jRadioButton4); //Add the radio buttons to the panel panel1.add(jRadioButton1); panel1.add(jRadioButton2); panel1.add(jRadioButton3); panel1.add(jRadioButton4); //Create the tabs tabbedPane.add("Tab 1", panel1); tabbedPane.add("Tab 2", new JPanel()); tabbedPane.add("Tab 3", new JPanel()); this.getContentPane().add(tabbedPane); setSize(200, 200); } public static void main(String[] args) { try { //Tell the UIManager to use the platform look and feel UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { e.printStackTrace(); } XPTabDemo demo = new XPTabDemo(); demo.setVisible(true); } }
discuss this topic to forum



