Java by API/javax.swing/JTabbedPane

Материал из Java эксперт
Версия от 14:20, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Содержание

JTabbedPane: addChangeListener(ChangeListener l)

 
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    String titles[] = { "A", "B", "C", "D", "E", "F" };
    int mnemonic[] = { KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_E,
        KeyEvent.VK_F };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i], mnemonic[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);
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
  static void add(JTabbedPane tabbedPane, String label, int mnemonic) {
    int count = tabbedPane.getTabCount();
    JButton button = new JButton(label);
    tabbedPane.addTab(label, button);
    tabbedPane.setMnemonicAt(count, mnemonic);
  }
}





JTabbedPane: addTab(String title, Component component)

 
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class MainClass {
  static void addIt(JTabbedPane tabbedPane, String text) {
    JLabel label = new JLabel(text);
    JButton button = new JButton(text);
    JPanel panel = new JPanel();
    panel.add(label);
    panel.add(button);
    tabbedPane.addTab(text, panel);
  }
  public static void main(String args[]) {
    JFrame f = new JFrame("JTabbedPane Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTabbedPane tabbedPane = new JTabbedPane();
    addIt(tabbedPane, "Tab One");
    addIt(tabbedPane, "Tab Two");
    addIt(tabbedPane, "Tab Three");
    f.add(tabbedPane, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setVisible(true);
  }
}





JTabbedPane: addTab(String title, Icon icon, Component component, String tip)

 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class Main extends JFrame {
  private JTextField textfield = new JTextField();
  public static void main(String[] args) {
    Main that = new Main();
    that.setVisible(true);
  }
  public Main() {
    setSize(450, 350);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(textfield, BorderLayout.SOUTH);
    JMenuBar mbar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menu.add(new JCheckBoxMenuItem("Check Me"));
    menu.addSeparator();
    JMenuItem item = new JMenuItem("Exit");
    item.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    });
    menu.add(item);
    mbar.add(menu);
    setJMenuBar(mbar);
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("Button", new TabIcon(), new JButton(""), "Click here for Button demo");
  }
}
class TabIcon implements Icon {
  public int getIconWidth() {
    return 16;
  }
  public int getIconHeight() {
    return 16;
  }
  public void paintIcon(Component c, Graphics g, int x, int y) {
    g.setColor(Color.black);
    g.fillRect(x + 4, y + 4, getIconWidth() - 8, getIconHeight() - 8);
    g.setColor(Color.cyan);
    g.fillRect(x + 6, y + 6, getIconWidth() - 12, getIconHeight() - 12);
  }
}





JTabbedPane: getComponentAt(int index)

 
import java.awt.ruponent;
import javax.swing.Icon;
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    int count = pane.getTabCount();
    for (int i = 0; i < count; i++) {
      String label = pane.getTitleAt(i);
      Icon icon = pane.getIconAt(i);
      String tooltip = pane.getToolTipTextAt(i);
      boolean enabled = pane.isEnabledAt(i);
      int keycode = pane.getMnemonicAt(i);
      Component comp = pane.getComponentAt(i);
    }
  }
}





JTabbedPane: getDisabledIconAt(int index)

 
import java.awt.Color;
import java.awt.ruponent;
import javax.swing.Icon;
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    int src = pane.getTabCount() - 1;
    int dst = 0;
    Component comp = pane.getComponentAt(src);
    String label = pane.getTitleAt(src);
    Icon icon = pane.getIconAt(src);
    Icon iconDis = pane.getDisabledIconAt(src);
    String tooltip = pane.getToolTipTextAt(src);
    boolean enabled = pane.isEnabledAt(src);
    int keycode = pane.getMnemonicAt(src);
    int mnemonicLoc = pane.getDisplayedMnemonicIndexAt(src);
    Color fg = pane.getForegroundAt(src);
    Color bg = pane.getBackgroundAt(src);
    pane.remove(src);
    pane.insertTab(label, icon, comp, tooltip, dst);
    pane.setDisabledIconAt(dst, iconDis);
    pane.setEnabledAt(dst, enabled);
    pane.setMnemonicAt(dst, keycode);
    pane.setDisplayedMnemonicIndexAt(dst, mnemonicLoc);
    pane.setForegroundAt(dst, fg);
    pane.setBackgroundAt(dst, bg);
  }
}





JTabbedPane: getDisplayedMnemonicIndexAt(int tabIndex)

 
import java.awt.Color;
import java.awt.ruponent;
import javax.swing.Icon;
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    int src = pane.getTabCount() - 1;
    int dst = 0;
    Component comp = pane.getComponentAt(src);
    String label = pane.getTitleAt(src);
    Icon icon = pane.getIconAt(src);
    Icon iconDis = pane.getDisabledIconAt(src);
    String tooltip = pane.getToolTipTextAt(src);
    boolean enabled = pane.isEnabledAt(src);
    int keycode = pane.getMnemonicAt(src);
    int mnemonicLoc = pane.getDisplayedMnemonicIndexAt(src);
    Color fg = pane.getForegroundAt(src);
    Color bg = pane.getBackgroundAt(src);
    pane.remove(src);
    pane.insertTab(label, icon, comp, tooltip, dst);
    pane.setDisabledIconAt(dst, iconDis);
    pane.setEnabledAt(dst, enabled);
    pane.setMnemonicAt(dst, keycode);
    pane.setDisplayedMnemonicIndexAt(dst, mnemonicLoc);
    pane.setForegroundAt(dst, fg);
    pane.setBackgroundAt(dst, bg);
  }
}





JTabbedPane: getIconAt(int index)

 
import java.awt.ruponent;
import javax.swing.Icon;
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    int count = pane.getTabCount();
    for (int i = 0; i < count; i++) {
      String label = pane.getTitleAt(i);
      Icon icon = pane.getIconAt(i);
      String tooltip = pane.getToolTipTextAt(i);
      boolean enabled = pane.isEnabledAt(i);
      int keycode = pane.getMnemonicAt(i);
      Component comp = pane.getComponentAt(i);
    }
  }
}





JTabbedPane: getMnemonicAt(int tabIndex)

 
import java.awt.ruponent;
import javax.swing.Icon;
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    int count = pane.getTabCount();
    for (int i = 0; i < count; i++) {
      String label = pane.getTitleAt(i);
      Icon icon = pane.getIconAt(i);
      String tooltip = pane.getToolTipTextAt(i);
      boolean enabled = pane.isEnabledAt(i);
      int keycode = pane.getMnemonicAt(i);
      Component comp = pane.getComponentAt(i);
    }
  }
}





JTabbedPane: getTitleAt(int index)

 
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    String titles[] = { "A", "B", "C", "D", "E", "F" };
    int mnemonic[] = { KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_E,
        KeyEvent.VK_F };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i], mnemonic[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);
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
  static void add(JTabbedPane tabbedPane, String label, int mnemonic) {
    int count = tabbedPane.getTabCount();
    JButton button = new JButton(label);
    tabbedPane.addTab(label, button);
    tabbedPane.setMnemonicAt(count, mnemonic);
  }
}





JTabbedPane: getToolTipTextAt(int index)

 
import java.awt.ruponent;
import javax.swing.Icon;
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    int count = pane.getTabCount();
    for (int i = 0; i < count; i++) {
      String label = pane.getTitleAt(i);
      Icon icon = pane.getIconAt(i);
      String tooltip = pane.getToolTipTextAt(i);
      boolean enabled = pane.isEnabledAt(i);
      int keycode = pane.getMnemonicAt(i);
      Component comp = pane.getComponentAt(i);
    }
  }
}





JTabbedPane: indexOfComponent(Component component)

 
    
import javax.swing.JButton;
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    JButton component = new JButton("button");
    int index = pane.indexOfComponent(component);
    if (index < 0) {
      // The tab could not be found
    }
  }
}





JTabbedPane: indexOfTab(String title)

 
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    // Get the index of the first tab that matches a label
    String label = "Tab Label";
    int index = pane.indexOfTab(label);
  }
}





JTabbedPane: insertTab(String title, Icon icon, Component component, String tip, int index)

 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    JButton component = new JButton("button");
    int index = 1;
    pane.insertTab("label", new ImageIcon("icon.png"), component, "tooltip", index);
  }
}





JTabbedPane: isEnabledAt(int index)

 

import javax.swing.JButton;
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    pane.addTab("Tab Label", new JButton("Button"));
    // Get index of the new tab
    int index = pane.getTabCount() - 1;
    // Determine whether the tab is enabled
    boolean enabled = pane.isEnabledAt(index);
    // Disable the tab
    pane.setEnabledAt(index, false);
  }
}





JTabbedPane: removeAll()

 
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
public class Main {
  static void add(JTabbedPane tabbedPane, String label, int mnemonic) {
    int count = tabbedPane.getTabCount();
    JButton button = new JButton(label);
    tabbedPane.addTab(label, new ImageIcon("yourFile.gif"), button, label);
    tabbedPane.setMnemonicAt(count, mnemonic);
  }
  public static void main(String args[]) {
    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" };
    int mnemonic[] = { KeyEvent.VK_G, KeyEvent.VK_S, KeyEvent.VK_C, KeyEvent.VK_O, KeyEvent.VK_P,
        KeyEvent.VK_A };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i], mnemonic[i]);
    }
    tabbedPane.removeAll();
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}





JTabbedPane: remove(Component component)

 
import javax.swing.JButton;
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    JButton component = new JButton("button");
    // Remove the last tab
    pane.remove(pane.getTabCount() - 1);
    // Remove the tab with the specified child component
    pane.remove(component);
    // Remove all the tabs
    pane.removeAll();
  }
}





JTabbedPane: removeTabAt(int index)

 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class Main {
  private JFrame frame = new JFrame();
  private JTabbedPane tabbedPane = new JTabbedPane();
  private JButton addTabButton = new JButton("Add Tab");
  private ImageIcon closeXIcon = new ImageIcon("C:/CloseX.gif");
  private Dimension closeButtonSize;
  private int tabCounter = 0;
  public Main() {
    addTabButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        add();
      }
    });
    closeButtonSize = new Dimension(closeXIcon.getIconWidth() + 2, closeXIcon.getIconHeight() + 2);
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.add(addTabButton, BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setMinimumSize(new Dimension(300, 300));
    frame.setVisible(true);
  }
  public void add() {
    final JPanel content = new JPanel();
    JPanel tab = new JPanel();
    tab.setOpaque(false);
    JLabel tabLabel = new JLabel("Tab " + (++tabCounter));
    JButton tabCloseButton = new JButton(closeXIcon);
    tabCloseButton.setPreferredSize(closeButtonSize);
    tabCloseButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        int closeTabNumber = tabbedPane.indexOfComponent(content);
        tabbedPane.removeTabAt(closeTabNumber);
      }
    });
    tab.add(tabLabel, BorderLayout.WEST);
    tab.add(tabCloseButton, BorderLayout.EAST);
    tabbedPane.addTab(null, content);
    tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, tab);
  }
  public static void main(String[] args) {
    Main main = new Main();
  }
}





JTabbedPane.SCROLL_TAB_LAYOUT

 
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
public class Main {
  static void add(JTabbedPane tabbedPane, String label, int mnemonic) {
    int count = tabbedPane.getTabCount();
    JButton button = new JButton(label);
    tabbedPane.addTab(label, new ImageIcon("yourFile.gif"), button, label);
    tabbedPane.setMnemonicAt(count, mnemonic);
  }
  public static void main(String args[]) {
    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" };
    int mnemonic[] = { KeyEvent.VK_G, KeyEvent.VK_S, KeyEvent.VK_C, KeyEvent.VK_O, KeyEvent.VK_P,
        KeyEvent.VK_A };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i], mnemonic[i]);
    }
    tabbedPane.removeAll();
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}





JTabbedPane: setBackgroundAt(int index, Color background)

 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
public class Main {
  static Color colors[] = { Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE,
      Color.MAGENTA };
  static void add(JTabbedPane tabbedPane, String label, int mnemonic) {
    int count = tabbedPane.getTabCount();
    JButton button = new JButton(label);
    tabbedPane.addTab(label, new ImageIcon("yourFile.gif"), button, label);
    tabbedPane.setMnemonicAt(count, mnemonic);
  }
  public static void main(String args[]) {
    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTabbedPane tabbedPane = new JTabbedPane();
    String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" };
    int mnemonic[] = { KeyEvent.VK_G, KeyEvent.VK_S, KeyEvent.VK_C, KeyEvent.VK_O, KeyEvent.VK_P,
        KeyEvent.VK_A };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i], mnemonic[i]);
      tabbedPane.setBackgroundAt(i,colors[i]);
    }
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}





JTabbedPane: setDisabledIconAt(int index, Icon disabledIcon)

 
import java.awt.Color;
import java.awt.ruponent;
import javax.swing.Icon;
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    int src = pane.getTabCount() - 1;
    int dst = 0;
    Component comp = pane.getComponentAt(src);
    String label = pane.getTitleAt(src);
    Icon icon = pane.getIconAt(src);
    Icon iconDis = pane.getDisabledIconAt(src);
    String tooltip = pane.getToolTipTextAt(src);
    boolean enabled = pane.isEnabledAt(src);
    int keycode = pane.getMnemonicAt(src);
    int mnemonicLoc = pane.getDisplayedMnemonicIndexAt(src);
    Color fg = pane.getForegroundAt(src);
    Color bg = pane.getBackgroundAt(src);
    pane.remove(src);
    pane.insertTab(label, icon, comp, tooltip, dst);
    pane.setDisabledIconAt(dst, iconDis);
    pane.setEnabledAt(dst, enabled);
    pane.setMnemonicAt(dst, keycode);
    pane.setDisplayedMnemonicIndexAt(dst, mnemonicLoc);
    pane.setForegroundAt(dst, fg);
    pane.setBackgroundAt(dst, bg);
  }
}





JTabbedPane: setDisplayedMnemonicIndexAt(int tabIndex, int mnemonicIndex)

 
import java.awt.Color;
import java.awt.ruponent;
import javax.swing.Icon;
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    int src = pane.getTabCount() - 1;
    int dst = 0;
    Component comp = pane.getComponentAt(src);
    String label = pane.getTitleAt(src);
    Icon icon = pane.getIconAt(src);
    Icon iconDis = pane.getDisabledIconAt(src);
    String tooltip = pane.getToolTipTextAt(src);
    boolean enabled = pane.isEnabledAt(src);
    int keycode = pane.getMnemonicAt(src);
    int mnemonicLoc = pane.getDisplayedMnemonicIndexAt(src);
    Color fg = pane.getForegroundAt(src);
    Color bg = pane.getBackgroundAt(src);
    pane.remove(src);
    pane.insertTab(label, icon, comp, tooltip, dst);
    pane.setDisabledIconAt(dst, iconDis);
    pane.setEnabledAt(dst, enabled);
    pane.setMnemonicAt(dst, keycode);
    pane.setDisplayedMnemonicIndexAt(dst, mnemonicLoc);
    pane.setForegroundAt(dst, fg);
    pane.setBackgroundAt(dst, bg);
  }
}





JTabbedPane: setEnabledAt(int index, boolean enabled)

 

import javax.swing.JButton;
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    pane.addTab("Tab Label", new JButton("Button"));
    // Get index of the new tab
    int index = pane.getTabCount() - 1;
    // Determine whether the tab is enabled
    boolean enabled = pane.isEnabledAt(index);
    // Disable the tab
    pane.setEnabledAt(index, false);
  }
}





JTabbedPane: setForegroundAt(int index, Color foreground)

 
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    pane.setForeground(Color.YELLOW);
    pane.setBackground(Color.MAGENTA);
    String label = "Tab Label";
    pane.addTab(label, new JButton("Button"));
    int index = pane.getTabCount() - 1;
    pane.setForegroundAt(index, Color.ORANGE);
    pane.setBackgroundAt(index, Color.GREEN);
  }
}





JTabbedPane: setMnemonicAt(int tabIndex, int mnemonic)

 
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    String titles[] = { "A", "B", "C", "D", "E", "F" };
    int mnemonic[] = { KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_E,
        KeyEvent.VK_F };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i], mnemonic[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);
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
  static void add(JTabbedPane tabbedPane, String label, int mnemonic) {
    int count = tabbedPane.getTabCount();
    JButton button = new JButton(label);
    tabbedPane.addTab(label, button);
    tabbedPane.setMnemonicAt(count, mnemonic);
  }
}





JTabbedPane: setSelectedIndex(int index)

 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
public class Main extends JFrame {
  private JTabbedPane tp;
  private int tabCounter = 0;
  public Main() {
    super("Browser");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JMenuBar mb = new JMenuBar();
    JMenu mFile = new JMenu("File");
    JMenuItem mi = new JMenuItem("Add Tab");
    ActionListener addTabl = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        addTab();
      }
    };
    mi.addActionListener(addTabl);
    mFile.add(mi);
    mb.add(mFile);
    setJMenuBar(mb);
    JPanel pnlURL = new JPanel();
    tp = new JTabbedPane();
    addTab();
    add(tp, BorderLayout.CENTER);
    setSize(300, 300);
    setVisible(true);
  }
  void addTab() {
    JEditorPane ep = new JEditorPane();
    ep.setEditable(false);
    tp.addTab(null, new JScrollPane(ep));
    JButton tabCloseButton = new JButton("Close");
    tabCloseButton.setActionCommand("" + tabCounter);
    ActionListener al;
    al = new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        JButton btn = (JButton) ae.getSource();
        String s1 = btn.getActionCommand();
        for (int i = 1; i < tp.getTabCount(); i++) {
          JPanel pnl = (JPanel) tp.getTabComponentAt(i);
          btn = (JButton) pnl.getComponent(0);
          String s2 = btn.getActionCommand();
          if (s1.equals(s2)) {
            tp.removeTabAt(i);
            break;
          }
        }
      }
    };
    tabCloseButton.addActionListener(al);
    if (tabCounter != 0) {
      JPanel pnl = new JPanel();
      pnl.setOpaque(false);
      pnl.add(tabCloseButton);
      tp.setTabComponentAt(tp.getTabCount() - 1, pnl);
      tp.setSelectedIndex(tp.getTabCount() - 1);
    }
    tabCounter++;
  }
  public static void main(String[] args) {
    new Main();
  }
}





JTabbedPane: setTabComponentAt(int index, Component component)

 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class Main {
  private JFrame frame = new JFrame();
  private JTabbedPane tabbedPane = new JTabbedPane();
  private JButton addTabButton = new JButton("Add Tab");
  private ImageIcon closeXIcon = new ImageIcon("C:/CloseX.gif");
  private Dimension closeButtonSize;
  private int tabCounter = 0;
  public Main() {
    addTabButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        add();
      }
    });
    closeButtonSize = new Dimension(closeXIcon.getIconWidth() + 2, closeXIcon.getIconHeight() + 2);
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.add(addTabButton, BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setMinimumSize(new Dimension(300, 300));
    frame.setVisible(true);
  }
  public void add() {
    final JPanel content = new JPanel();
    JPanel tab = new JPanel();
    tab.setOpaque(false);
    JLabel tabLabel = new JLabel("Tab " + (++tabCounter));
    JButton tabCloseButton = new JButton(closeXIcon);
    tabCloseButton.setPreferredSize(closeButtonSize);
    tabCloseButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        int closeTabNumber = tabbedPane.indexOfComponent(content);
        tabbedPane.removeTabAt(closeTabNumber);
      }
    });
    tab.add(tabLabel, BorderLayout.WEST);
    tab.add(tabCloseButton, BorderLayout.EAST);
    tabbedPane.addTab(null, content);
    tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, tab);
  }
  public static void main(String[] args) {
    Main main = new Main();
  }
}





JTabbedPane: setTabLayoutPolicy(int tabLayoutPolicy)

 
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    String titles[] = { "A", "B", "C", "D", "E", "F" };
    int mnemonic[] = { KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_E,
        KeyEvent.VK_F };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i], mnemonic[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);
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
  static void add(JTabbedPane tabbedPane, String label, int mnemonic) {
    int count = tabbedPane.getTabCount();
    JButton button = new JButton(label);
    tabbedPane.addTab(label, button);
    tabbedPane.setMnemonicAt(count, mnemonic);
  }
}





JTabbedPane: setTabPlacement(int tabPlacement)

 

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
public class Main {
  static void add(JTabbedPane tabbedPane, String label, int mnemonic) {
    int count = tabbedPane.getTabCount();
    JButton button = new JButton(label);
    tabbedPane.addTab(label, new ImageIcon("yourFile.gif"), button, label);
    tabbedPane.setMnemonicAt(count, mnemonic);
  }
  public static void main(String args[]) {
    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTabbedPane tabbedPane = new JTabbedPane();
    String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" };
    int mnemonic[] = { KeyEvent.VK_G, KeyEvent.VK_S, KeyEvent.VK_C, KeyEvent.VK_O, KeyEvent.VK_P,
        KeyEvent.VK_A };
    for (int i = 0, n = titles.length; i < n; i++) {
      add(tabbedPane, titles[i], mnemonic[i]);
    }
    tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}





JTabbedPane: setToolTipTextAt(int index, String toolTipText)

 

import javax.swing.JButton;
import javax.swing.JTabbedPane;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    String label = "Tab Label";
    String tooltip = "Tool Tip Text";
    pane.addTab(label, null, new JButton("Button"), tooltip);
    int index = pane.getTabCount() - 1;
    tooltip = pane.getToolTipTextAt(index);
    tooltip = "New Tool Tip Text";
    pane.setToolTipTextAt(index, tooltip);
  }
}