JTabbedPane - Stretching the tabs to the width of the pane

Hi all,

I'm trying to figure out how to stretch the tabs of a JTabbedPane to the width of the pane. For instance, when I create a simple tabbed pane, the tabs resize themselves to fit the text they hold. Is there a way to evenly stretch the tabs out so they span the top of the pane?

Thanks!

[312 byte] By [Jason102a] at [2007-10-2 20:27:19]
# 1
Add your tabbed pane to a panel with GridLayout.
ajitrma at 2007-7-13 23:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 2
Hey thanks I'll try that out. Now that I think of it, that sort of makes sense!
Jason102a at 2007-7-13 23:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 3
you want to stretch the tab part, not the pane part?if so, try this http://forum.java.sun.com/thread.jspa?forumID=57&threadID=729622
Michael_Dunna at 2007-7-13 23:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 4

It looks like I still have a problem. You see my tabbed pane contains 6 tabs - 3 on each level (2 levels of tabs). I tried ajitrm's way of putting the pane into a panel using GridLayout, but that didn't do anything. The parameters of this layout was set to (1,1), because that was the only thing I could think of - this panel only consists of 1 JTabbedPane. ajitrm, do I set the parameters to be as if the 2 levels of tabs were components (2,3)? And Michael, I see what you are doing for your 1 level of tabs, but how could I make it work for my 2 levels? I can't cram all 6 tabs into 1 level.

Again, thanks for any more help!

Jason102a at 2007-7-13 23:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 5

this is totally untested, but it might be worth trying to calculate the tab width based on 2 rows

return (int)((tabPaneWidth - margin.left - margin.right)/tp.getTabCount());

becomes

return (int)(((tabPaneWidth - margin.left - margin.right)/tp.getTabCount())/2);

you'd also need to check what happens if tabCount is an odd number

may also need to tweak the left/right margins to allow for 2 rows

Message was edited by:

Michael_Dunn

the 2 is in the wrong place

should be the tabCount/2, not calculatedWidth/2

Michael_Dunna at 2007-7-13 23:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 6

Thanks, Michael! The only thing now is setting the tabbed pane's UI to look like the default one. In the process of setting the tab's widths, the code also changes the tab's look. I'd like to be able to set the look back to the default look because I have other tabbed panes in my program, and having one pane looking different I think would make the thing look odd. How do I do this?

Jason102a at 2007-7-13 23:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 7

try changing this

class MyUI extends javax.swing.plaf.basic.BasicTabbedPaneUI

to

class MyUI extends javax.swing.plaf.metal.MetalTabbedPaneUI

other than that, you could try setting the ui at the top of the constructor,

followed by settng the lookandfeel, followed by SwingUtilities.updateComponentTreeUI(..)

Michael_Dunna at 2007-7-13 23:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 8

I have one more question, but if you can't answer it, it's no big deal. Usually a tabbed pane consisting of 2 rows draws the clicked on tab's row to the front. For instance, lets say the top row has a tab named "1" and the bottom (up front) row has a tab named "2". When you click on 1, that tab's row is then brought up to the front or bottom, switching places with 2's row. When I use your code, the rows don't bring themselves up to the front like that when a tab of theirs is clicked on. Like I said, it's no big deal if you can't help me on this one, but I'd like to have my tabbed pane behave as normally as all the other default JTabbedPanes. Thanks!

Jason102a at 2007-7-13 23:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 9

I have one more question, but if you can't answer it, it's no big deal. Usually a tabbed pane consisting of 2 rows draws the clicked on tab's row to the front. For instance, lets say the top row has a tab named "1" and the bottom (up front) row has a tab named "2". When you click on 1, that tab's row is then brought up to the front or bottom, switching places with 2's row. When I use your code, the rows don't bring themselves up to the front like that when a tab of theirs is clicked on. Like I said, it's no big deal if you can't help me on this one, but I'd like to have my tabbed pane behave as normally as all the other default JTabbedPanes. Thanks!

Jason102a at 2007-7-13 23:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 10
Whoa! Either I'm off my rocker or else this site has a glitch! When I clicked Post, it didn't do anything, so I tried again, and when I checked the thread, I had posted 2 of my replies! Sorry about that!
Jason102a at 2007-7-13 23:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 11
> Usually a tabbed pane consisting of 2 rows draws the clicked on tab's row to the frontdoesn't do that with the versions I'm using, can you post a small demo program to illustrate?(might be a setting I'm not seeing)
Michael_Dunna at 2007-7-13 23:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 12

Hmm... I swear I remember dealing specifically with Swing tabbed panes before that did this, but they don't do it now. Maybe one of the older JDKs had that sort of feature, or else I fooled my memories confusing the Window's tabbed panes with Java's. Those tabs do come to the front, and are also extended to the width of the pane. Oh well, its no big deal - I just thought it would make everything look a bit neater. Thanks anyway, Michael.

Jason102a at 2007-7-13 23:10:12 > top of Java-index,Desktop,Core GUI APIs...
# 13
I have 14 tabs in JTabbedPanel, I need 6 tabs in the first line and 8 tabs in the second line because they are not same width. I really don't know how to align them nicely. I really appreciate your help!
xiaohuimaa at 2007-7-13 23:10:12 > top of Java-index,Desktop,Core GUI APIs...