• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Hide tabs in JTabbedPane

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried setVisible?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> An easy way to do this is to use absolute layout....

and then the nightmare begins.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tabPane.getTabAt(0).setVisible(false);
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:)
 
Marshal
Posts: 80943
522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just use a CardLayout already.
 
Mikhail Malinin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic