Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Get size of Text in a JLabel  RSS feed

 
E Hill
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am attempting to produce a GUI representation of a tree data structure, with layers varying between round and square nodes.

Im producing my own components for this however im having trouble sizing them. Within the components I'm passing in a JLabel that I can use to get the text from to paint, I was also planning on using the label to get the dimensions of the text and size the box appropriately. The only way to get the size of the JLabel using getWidth() and getHeight() appears to be after the frame has been set visible.

Is there anyway to get the potential size of a component before it is visible? I can show and hide the frame directly afterwards and build it up gradually, however I would prefer a more efficient way if there were one.

Thanks for any help
Elliottjhill
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A more direct way to find out how big some text will be on the screen would be to use the FontMetrics class.
 
E Hill
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Worked a treat, thanks. Knew there must have been an easier way to do that.

Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!