Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hide tabs in JTabbedPane

 
Sean O'Donnell
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried setVisible?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> An easy way to do this is to use absolute layout....

and then the nightmare begins.
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tabPane.getTabAt(0).setVisible(false);
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
gary eberhart
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48967
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use a CardLayout already.
 
Mikhail Malinin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic