Add Button to tab bar : JTabbedPane « Swing « Java Tutorial






Add Button to tab bar
import java.awt.BorderLayout;
import java.awt.Component;
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.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

public class AddButtonToTabBar extends JFrame {
  private JTabbedPane tp;

  private JLabel lblStatus;

  private int tabCounter = 0;

  public AddButtonToTabBar() {
    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();
    getContentPane().add(tp, BorderLayout.CENTER);

    lblStatus = new JLabel(" ");
    getContentPane().add(lblStatus, BorderLayout.SOUTH);

    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 AddButtonToTabBar();

  }
}








14.51.JTabbedPane
14.51.1.First usage of JTabbedPane
14.51.2.Adding and Removing TabsAdding and Removing Tabs
14.51.3.Get the index of the first tab that matches an icon
14.51.4.Changing tab's title, icon, mnemonic, tooltip, or component on a particular tab with one of the setXXXAt() methodsChanging tab's title, icon, mnemonic, tooltip, or component  on a particular tab with one of the setXXXAt() methods
14.51.5.Adds tool tips to a table using a rendererAdds tool tips to a table using a renderer
14.51.6.Specifying a tab's location: TOP, BOTTOM, LEFT, or RIGHTSpecifying a tab's location: TOP, BOTTOM, LEFT, or RIGHT
14.51.7.Changing background, foreground and iconChanging background, foreground and icon
14.51.8.To remove a tab, you can remove a specific tab with removeTabAt(int index), remove(int index), or remove(Component component)To remove a tab, you can remove a specific tab with  removeTabAt(int index), remove(int index), or remove(Component component)
14.51.9.Listening for Selected Tab ChangesListening for Selected Tab Changes
14.51.10.TabLayout Policy: SCROLL_TAB_LAYOUT or WRAP_TAP_LAYOUTTabLayout Policy: SCROLL_TAB_LAYOUT or WRAP_TAP_LAYOUT
14.51.11.Add Components to JTabbedPane
14.51.12.New Methods in the JTabPane Component (Add component to JTabPane)
14.51.13.JTabPane with TextField in the tabJTabPane with TextField in the tab
14.51.14.Add user icon to tab panelAdd user icon to tab panel
14.51.15.Add Button to tab barAdd Button to tab bar
14.51.16.Add a tab with a label taken from the name of the component
14.51.17.Add a tab with a label at the end of all tabs
14.51.18.Add a tab with a label and icon at the end of all tabs
14.51.19.Add a tab with a label, icon, and tool tip at the end of all tabs
14.51.20.Getting the Tabs in a JTabbedPane Container
14.51.21.Enable Scrolling Tabs in a JTabbedPane Container
14.51.22.Enabling the Selection of a Tab in a JTabbedPane Container Using a Keystroke
14.51.23.Enabling and Disabling a Tab in a JTabbedPane Container
14.51.24.Setting the Color of a Tab in a JTabbedPane Container
14.51.25.Setting the Tool Tip for a Tab in a JTabbedPane Container
14.51.26.Setting the Location of the Tabs in a JTabbedPane Container
14.51.27.Determining When the Selected Tab Changes in a JTabbedPane Container
14.51.28.Moving a Tab in a JTabbedPane Container
14.51.29.Insert a tab after the first tab
14.51.30.Get the index of the tab by matching the child component
14.51.31.Getting and Setting the Selected Tab in a JTabbedPane Container
14.51.32.Setting the Size of the Divider in a JSplitPane Container
14.51.33.Customizing a JTabbedPane Look and Feel