views:

1190

answers:

3

I'm trying to get a JTabbedPane where all tabs (the actual tabs, not the components) have the same width (either the minimum width needed for the widest label or a constant width).

I've tried to override BasicTabbedPaneUI.getTabBounds(int tabIndex, Rectangle dest), but apparently this method isn't used by the painting methods of BasicTabbedPaneUI, instead it uses a rects array to determine the tabs size.

My next approach would be to override JTabbedPane.insertTab(String title, Icon icon, Component component, String tip, int index) and setting the preferred size of the label component, but this doesn't seem very elegant and I'm not even sure it would work at all.

Is there a way to achieve this?

+2  A: 

I think it's not as complicated as you've done. Just use setTabComponentAt() with a JLabel on which you've set preferred size.

Software Monkey
That works, thanks. Unfortunately I have to set all other parameters of the label component (color, font size etc.) myself. I thought of retrieving the label via getTabComponentAt(), but it returns null all the time.
Ole
Yep, that's because the getter only gets a label that was previously set (I think the doco specifically mentions that).
Software Monkey
A: 

Hi:

I came across this article (http://blog.elevenworks.com/?p=4) that goes quite in-depth about how to change the appearance of a JTabbedPane. Step 7 is "Specify the size of the tabs" and seems to be what you are trying to do. See if it helps you.

The link seems to be dead
Stuart Axon
+1  A: 

Hi

The answer is simple. When we put the name for the tab, just format the name using html. say

tp - JTabbedPane object

tp.addTab("<html><body><table width='150'>Name</table></body></html>",Componentobject)
Arun Kumar S
This sounds very good. I've seen other situations where html was the only way to achieve something in swing (e.g. line wrap on tooltips). I'll definitely give it a try.
Ole