Stephane Grenier
Stephane Grenier

Reputation: 15925

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.

Upvotes: 4

Views: 19997

Answers (4)

Lucas Motta
Lucas Motta

Reputation: 145

This work in my project.

this.TabbedPane.setEnabledAt(1, false);

Upvotes: 2

Stephane Grenier
Stephane Grenier

Reputation: 15925

The only way is to remove it when you don't want to see it, and re-add it later when you do want it visible.

Upvotes: 4

Tarun Gupta
Tarun Gupta

Reputation: 1242

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 = ...

Upvotes: 0

vels4j
vels4j

Reputation: 11298

I think this can be done only by custom component.

Here is an api for HideableTabbedPane try that

Upvotes: 0

Related Questions