To remove tabs from JTabbedPane
we can use the following remove methods:
remove(int index)
method to remove a tab at the specified index.remove(Component component)
method to remove a tab that has the specified child component.removeAll()
method to remove all tabs.
package org.kodejava.swing;
import javax.swing.*;
import java.awt.*;
public class TabbedPaneRemoveTab extends JPanel {
public TabbedPaneRemoveTab() {
initializeUI();
}
public static void showFrame() {
JPanel panel = new TabbedPaneRemoveTab();
panel.setOpaque(true);
JFrame frame = new JFrame("JTabbedPane Demo");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(TabbedPaneRemoveTab::showFrame);
}
private void initializeUI() {
final JTabbedPane pane = new JTabbedPane(JTabbedPane.LEFT);
pane.addTab("A Tab", new JPanel());
pane.addTab("B Tab", new JPanel());
JPanel tabPanel = new JPanel();
pane.addTab("C Tab", tabPanel);
pane.addTab("D Tab", new JPanel());
pane.addTab("E Tab", new JPanel());
// Remove the last tab from JTabbedPane
pane.remove(pane.getTabCount() - 1);
// Remove tab that contains a tabPanel component which is
// the C Tab.
pane.remove(tabPanel);
JButton button = new JButton("Remove All Tabs");
button.addActionListener(e -> {
// Remove all tabs from JTabbedPane
pane.removeAll();
});
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(500, 200));
this.add(pane, BorderLayout.CENTER);
this.add(button, BorderLayout.SOUTH);
}
}
The output of the code snippet above is:
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
Hello friends, I have shared a java swing library of my own on the web.
Visit the following for more reference, thanks.
https://www.yoyoapi.com/projects/jswing/JSwing-quick-start-guide.html