• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTabbedPane tab title layouts - button title button

 
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks

I've got a JTabbedPane with tab placement set to LEFT, so the tab titles are arranged in rows down the left hand side. Each tab title consist of 2 same size buttons and variable size text.

I want the title layout to be such that the buttons are at the left and right tab edges, with the text centered in the middle:

I naively thought using BorderLayout for the the tabs' JPanels and EAST/CENTER/WEST placement would do it, but that gives me something like:

while GridLayout(1, 3) gives me:

Is there an 'easy' way to get what I'm after, or do I have to try to get to grips with GridBagLayout/Constraints (again)?

(NB. in the above diagrams, each row is a separate tab/JPanel.)

Cheers
John
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(NB. in the above diagrams, each row is a separate tab/JPanel.)


I guess that's the reason why BorderLayout doesn't work - if I could make all the JPanels the same width (ie. the width of the tab/panel with the longest text), the buttons would line up?
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok - I've found this which implies the answer isn't straightforward.

However I appear to be making some progress by getting the size of all the tab components, finding the widest, then setting the preferred size of all the components to that size...
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Success



(Actually I just set the preferred sizes of the text labels rather than the whole of the tab components, and decided that text/button/button works better than button/text/button.)
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi John,

I see the topic is resolved, however I just finished a quick&dirty try, that delivers what you asked in your opening post. I'll give it anyway, otherwise I would have wasted some time      
For each tab I created a JPanel, to which I added an icon, another JPanel with flowLayout.CENTER and a preferred size, containing a JLabel of various size, and finally an icon again. I'm thinking of a way to get rid of the fixed size of that middle panel. Well, for what it's worth...
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Piet, and apologies for answering my own question

I can see similarities with my solution; the main problem as you say would be the hard-coded sizes (depends on file data in my app).
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, there is a way to get rid of it.
If you have a Graphics g, then set the font that you want to use, and call g.getFontMetrics(). This returns a FontMetrics fm, then call fm.getStringBounds(String s, g). See the API. That String should be the longest string that you expect. It returns a Rectangle2D that is large enough to contain that String. Admittedly, I haven't tried this out yet, so: no guarantees!
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry - I meant I had solved it in my solution. I use JLabels for the text, so it was just a case of setting the preferred size of all the JLabels to that of the widest.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see... that would simplify the code a lot.
I did try, at first, to use the preferred size, but I noticed that changing the text of a JLabel did alter the size of it. But I now realize what I did wrong, so I learnt something new! As they say: (almost) never too old to learn. Thanks for your comments!
 
reply
    Bookmark Topic Watch Topic
  • New Topic