TabbedPane Sample 2 : TabbedPane « Swing JFC « Java






TabbedPane Sample 2

TabbedPane Sample 2
 
/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski     
ISBN: 1-893115-78-X
Publisher: APress
*/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Polygon;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TabbedPaneSample {
  static Color colors[] = { Color.red, Color.orange, Color.yellow,
      Color.green, Color.blue, Color.magenta };

  static void add(JTabbedPane tabbedPane, String label) {
    int count = tabbedPane.getTabCount();
    JButton button = new JButton(label);
    button.setBackground(colors[count]);
    tabbedPane.addTab(label, new DiamondIcon(colors[count]), button, label);
  }

  public static void main(String args[]) {
    String title = (args.length == 0 ? "Tabbed Pane Sample" : args[0]);
    JFrame frame = new JFrame(title);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane();
    String titles[] = { "General", "Security", "Content", "Connection",
        "Programs", "Advanced" };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i]);
    }

    ChangeListener changeListener = new ChangeListener() {
      public void stateChanged(ChangeEvent changeEvent) {
        JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent
            .getSource();
        int index = sourceTabbedPane.getSelectedIndex();
        System.out.println("Tab changed to: "
            + sourceTabbedPane.getTitleAt(index));
      }
    };
    tabbedPane.addChangeListener(changeListener);

    Container contentPane = frame.getContentPane();
    contentPane.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}

class DiamondIcon implements Icon {
  private Color color;

  private boolean selected;

  private int width;

  private int height;

  private Polygon poly;

  private static final int DEFAULT_WIDTH = 10;

  private static final int DEFAULT_HEIGHT = 10;

  public DiamondIcon(Color color) {
    this(color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  }

  public DiamondIcon(Color color, boolean selected) {
    this(color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  }

  public DiamondIcon(Color color, boolean selected, int width, int height) {
    this.color = color;
    this.selected = selected;
    this.width = width;
    this.height = height;
    initPolygon();
  }

  private void initPolygon() {
    poly = new Polygon();
    int halfWidth = width / 2;
    int halfHeight = height / 2;
    poly.addPoint(0, halfHeight);
    poly.addPoint(halfWidth, 0);
    poly.addPoint(width, halfHeight);
    poly.addPoint(halfWidth, height);
  }

  public int getIconHeight() {
    return height;
  }

  public int getIconWidth() {
    return width;
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    g.setColor(color);
    g.translate(x, y);
    if (selected) {
      g.fillPolygon(poly);
    } else {
      g.drawPolygon(poly);
    }
    g.translate(-x, -y);
  }
}

           
         
  








Related examples in the same category

1.Demonstrates the Tabbed PaneDemonstrates the Tabbed Pane
2.A demonstration of the tab wrapping property of JTabbedPaneA demonstration of the tab wrapping property of JTabbedPane
3.A quick test of the JTabbedPane componentA quick test of the JTabbedPane component
4.JTabbedPane class for displaying and manipulatingJTabbedPane class for displaying and manipulating
5.TabbedPane SampleTabbedPane Sample
6.TabbedPane DemoTabbedPane Demo
7.Laying Out a Screen with JTabbedPaneLaying Out a Screen with JTabbedPane
8.TabPane DemoTabPane Demo
9.Determining When the Selected Tab Changes in a JTabbedPane Container
10.Enable Scrolling Tabs in a JTabbedPane Container
11.Enabling the Selection of a Tab in a JTabbedPane Container Using a Keystroke
12.Setting the Color of a Tab in a JTabbedPane Container
13.Setting the Tool Tip for a Tab in a JTabbedPane Container
14.Enabling and Disabling a Tab in a JTabbedPane Container
15.Setting the Location of the Tabs in a JTabbedPane Container
16.Getting the Tabs in a JTabbedPane Container
17.Retrieve the index of a tab when needed
18.Get the index of the first tab that matches an icon
19.Get the index of the tab by matching the child component
20.Moving a Tab in a JTabbedPane Container
21.Removing a Tab in a JTabbedPane Container
22.Add a tab with a label taken from the name of the component
23.Add a tab with a label at the end of all tabs
24.Add a tab with a label and icon at the end of all tabs
25.Add a tab with a label, icon, and tool tip at the end of all tabs
26.Insert a tab after the first tab
27.Getting and Setting the Selected Tab in a JTabbedPane Container
28.Creating a JTabbedPane Container
29.Setting the Size of the Divider in a JSplitPane Container
30.This program demonstrates the tabbed pane component organizer.This program demonstrates the tabbed pane component organizer.