Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Hide tabs in JTabbedPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Hide tabs in JTabbedPane" Watch "Hide tabs in JTabbedPane" New topic
Author

Hide tabs in JTabbedPane

Sean O'Donnell
Greenhorn

Joined: Dec 27, 2005
Posts: 12
Greetings-
Plain and simple, is it possible to hide the tabs in a JTabbedPane (and if so, how)? Tabs are unnecessary as the users can select what panel they want to view via a JTree on the side. I am aware that there is a CardLayout that would suite my needs, but I would like to keep the JTabbedPane in place so that the tab visibility can be changed depending on user preference.

Thanks in advance for any help.
Sean
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Have you tried setVisible?


GenRocket - Experts at Building Test Data
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you can do something like this, which just doesn't paint the tabArea


problems are:
1)
needs additional code to paint the top tab border, where it joins tabArea
2)
can still click the 'invisible' tab area, to change tabs
3)
even though tabAreas are not shown, the space allocated remains, i.e. the
pane of the JTabbedPane is lower in its container than a cardlayout would be

seems it might be a lot easier to switch between cardLayout/JTabbedPane, as required
Sean O'Donnell
Greenhorn

Joined: Dec 27, 2005
Posts: 12
Thanks to both of you for your input. I have decided to implement both the JTabbedPane and the CardLayout, and let the user select which he or she would like to use.

Joel Bronte
Greenhorn

Joined: Aug 04, 2008
Posts: 1
Just in case someone else reads this post.

An easy way to do this is to use absolute layout and put the JTabbedPane inside a JPanel at location (0,-tabHeight)
or simply:

jPanel.setLayout(null);
jPanel.add(jTabbedPanel);
jTabbedPane.setLocation(0,-25);
jTabbedPane.setSize(sizeX,sizeY); //need setSize also since using null layout


NOTE: if window resizing is an issue you'll need to overwrite setSize on jPanel to change the size of jTabbedPane also when the user resizes.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> An easy way to do this is to use absolute layout....

and then the nightmare begins.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
Can only agree with Michael ... moving the tabs out of the visible area is definitely not a good solution.

Maybe have a look at how JTabbedPane handles the tabs internally and do something similar. What might possibly work is to keep all the tabs (JPanels?) in an arry and set the content of the displaying container (where the JTabbedPae used to be) as users click in the JTree? Or just move the JTabbedPane's tab location to the left and omit the JTree, it it's only a question of layout ...

Just my two cents ...
gary eberhart
Greenhorn

Joined: Jun 03, 2009
Posts: 2
tabPane.getTabAt(0).setVisible(false);
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Hi Gary,
Welcome to the Ranch.

If you have noticed, the original post is from 2007! I doubt if the original poster is waiting for the solution that long!
http://faq.javaranch.com/java/DontWakeTheZombies
Also the actual method is JTabbedPane#getTabComponentAt(int index).

Nevertheless, it's nice to have you here!


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
gary eberhart
Greenhorn

Joined: Jun 03, 2009
Posts: 2
Thanks for the information. We've sub classed JTabbedPane and my version(FlexJTabbedPane) has a getTabAt() LOL.... I'll be more careful next time. We likely added getTabAt() prior to Java 6 being released. I will remove it from my class now that you have enlightened me. Thanks.
Dmitry Gundorov
Greenhorn

Joined: Jan 18, 2010
Posts: 1
I've had the same problem and sorted it this way:

You can also get rid of the gray border around the tabbed pane by overriding method paintContentBorder(Graphics g, int tabPlacement, int selectedIndex).
Rob Clawson
Greenhorn

Joined: May 19, 2010
Posts: 2
Overriding the calculateTabAreaHeight method was perfect. Just what I needed. To make the display work properly though, I also had to override the paintTab method.

Thanks for the insight!
Mikhail Malinin
Greenhorn

Joined: Sep 12, 2013
Posts: 2
solution to issue 2("can still click the 'invisible' tab area, to change tabs") is overriding of BasicTabbedPaneUI.tabForCoordinate.
I place my own version of JTabbedPane_withoutPaintedTabs for these people who are in search yet
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Useful stuff, even after 3 years
I have gone back and taken some of the longer lines out of your code, because they can make it difficult to read.

And welcome to the Ranch
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Just use a CardLayout already.


luck, db
There are no new questions, but there may be new answers.
Mikhail Malinin
Greenhorn

Joined: Sep 12, 2013
Posts: 2
to Darryl Burke and Campbell Ritchie: you are correct , of course. But please read intently question from topic starter: "I am aware that there is a CardLayout that would suite my needs, but I would like to keep the JTabbedPane in place so that the tab visibility can be changed depending on user preference". I had the same requirements.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hide tabs in JTabbedPane