• Post Reply Bookmark Topic Watch Topic
  • New Topic

JToolBar width  RSS feed

 
Stephen Richards
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gday everyone from a new member. I'm currently attempting to learn Java at uni and here's one my teacher can't answer...

Is there a way to return the width of a JToolBar including buttons, etc? Basically i want to automatically size a JFrame to fit the width of the JToolBar. I haven't been able to find anything that either holds the width or can calculate it.

Thanks in advance!
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getHeight , getWidth

Inherited from the JComponent

// Mathias
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Methods getWidth, getHeight and getSize are all of a piece. They return
the component's current size, but only if that has been set! They
are properly set by their container's layout manager, but also by sticky-fingered
coders who setLayout(null).

What you want is getPreferredSize:
 
Stephen Richards
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help guys.

Mathias...already tried the get methods, as Jeff says they only return a value if one has been set, they don't automatically take the dimensions of the object.

Jeff...using the getPreferredSize nearly worked but not quite. It still cut off half of the last button for some reason.

But then i figured out i could do it like this anyway...

frame.pack();
frame.setSize(frame.getWidth(), 200); //wanted to set height manually

Don't know if this is the best way but it work! Thanks again, i'll be back
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!