How can I set some of the tabs in a JTabbedPane invisible? I tried using JTabbedPane#getTabComponentAt(index).setVisible(false);, but it throws a NullPointerException. I can disable the tabs, but not make them invisible.
SSCCE:
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
public class Main {
public static void main(String[] args) {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setPreferredSize(new Dimension(400, 100));
for (int i = 0; i < 7; i++)
tabbedPane.add("tab " + i, new JLabel("content " + i));
// this throws a NullPointerException
tabbedPane.getTabComponentAt(1).setVisible(false);
// this works
tabbedPane.setEnabledAt(1, false);
JFrame frame = new JFrame();
frame.setContentPane(tabbedPane);
frame.pack();
frame.setVisible(true);
}
}
I can not find out what I am doing wrong.
See the javadoc of the corresponding setter:
Sets the component that is responsible for rendering the title for the specified tab. A null value means JTabbedPane will render the title and/or icon for the specified tab. A non-null value means the component will render the title and JTabbedPane will not render the title and/or icon.
So the JTabbedPane#getTabComponentAt(index) method returns the Component used to render the tab if you set any, otherwise it uses a label and/or an icon.
Not sure whether you can make a tab invisible, but sure as hell you can remove them and insert them. That might be an acceptable solution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With