'How to hide tabbed panel in JTabbedPane?

I have:

JTabbedPane jtabbedPane = new JTabbedPane();
jTabbedPane.addTab("Tab 1", panel1);
jTabbedPane.addTab("Tab 2", panel2);
jTabbedPane.addTab("Tab 3", panel3);

What I want to do is hide Tab 2 when a condition occurs (say the user isn't permitted to access that tabbed panel.

Yes I know you can do:

jtabbedPane.setEnabled(1, false); // disable Tab 2

which will gray it out, but I want to completely hide it so that the user doesn't even know it's even a possibility in the software. They shouldn't be even aware that it exists.

I do NOT want to do

jtabbedPane.remove(1); // remove Tab 2

because I then have to remove/add on a regular basis.



Solution 1:[1]

This work in my project.

this.TabbedPane.setEnabledAt(1, false);

Solution 2:[2]

I think this can be done only by custom component.

Here is an api for HideableTabbedPane try that

Solution 3:[3]

Solution 1:- why don't you just start x at the value of 1, so it skips 0, instead of starting at 0 and checking for x>1...

Solution 2:- [http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html#tabapi][1]

[1]: http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html#tabapi use that link.

Solution 3:- You can do something like this, which just doesn't paint the tabArea

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
class Testing { 
public void buildGUI() { 
JTabbedPane tp = new JTabbedPane(); 
tp.addTab("A",getPanel("A")); 
tp.addTab("B",getPanel("B")); 
tp.addTab("C",getPanel("C")); 
tp.setUI(new javax.swing.plaf.metal.MetalTabbedPaneUI(){ 
protected void paintTabArea(Graphics g,int tabPlacement,int selectedIndex){} }); 
JFrame f = new JFrame(); 
f.getContentPane().add(tp); 
f.pack(); 
f.setLocationRelativeTo(null); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
f.setVisible(true);
 } 
public JPanel getPanel(String tabText)
 { 
JPanel p = ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lucas Motta
Solution 2 vels4j
Solution 3 Tarun Gupta