Java Swing How to - Remove last tab of JTabbedPane








Question

We would like to know how to remove last tab of JTabbedPane.

Answer

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
//from   w  ww .j  a v a  2  s.  co m
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class Main extends JFrame {
  JTabbedPane tabbedPane = null;
  MouseListener tabbedPaneMouseListener = null;
  ArrayList<String> nameOfTab = new ArrayList<String>();
  static int counter = 1;

  public Main() {
    setSize(500, 500);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLayout(new GridLayout(0, 1));
    JPanel buttonPanel = new JPanel();

    JButton addButton = new JButton("add");
    addButton.addActionListener(e -> addElementToArrayList());
    JButton removeButton = new JButton("remove");
    removeButton.addActionListener(e -> removeElementFromArrayList());

    tabbedPane = new JTabbedPane();
    tabbedPaneMouseListener = (new MouseAdapter() {

      public void mouseClicked(MouseEvent e) {
        if (SwingUtilities.isLeftMouseButton(e)) {
          if (e.getClickCount() == 1) {
            System.out.println("Do Something");
          }
        }
      }
    });
    tabbedPane.addMouseListener(tabbedPaneMouseListener);

    buttonPanel.add(addButton);
    buttonPanel.add(removeButton);
    add(buttonPanel);
    this.add(tabbedPane);
  }
  void addElementToArrayList() {
    nameOfTab.add("" + counter++ + ". element");
    updateTabbedPane();
  }
  void removeElementFromArrayList() {
    if (nameOfTab.size() > 0) {
      nameOfTab.remove(0);
    }
    updateTabbedPane();
  }
  public void updateTabbedPane() {
    tabbedPane.removeMouseListener(tabbedPaneMouseListener);
    while (tabbedPane.getTabCount() > 0) {
      tabbedPane.remove(0);
    }
    for (int i = 0; i < nameOfTab.size(); i++) {
      tabbedPane.addTab(nameOfTab.get(i), new JLabel(nameOfTab.get(i)));
    }
    tabbedPane.addMouseListener(tabbedPaneMouseListener);
  }
  public static void main(String args[]) {
    new Main().setVisible(true);
  }
}