• 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

How to make JLabel to have size to accomodate text only?

 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

I am doing my own tooltip like that for eclipse doc.
For this I use JDialog which has child JLabel wrapped inside JScrollPane.
Of course as user moves cursor I need to change text of JLabel.
The most important is that I want JLabel to have just enough size to accomodate text only.
And if text is too big and size of JLabel should be very big then I want to decrease size of JLabel.

The problem for me is how to calculate and resize JDialog with JLabel so that JLabel is large enough to accomodate its text.

I guess I need to calculate size of JLabel using its text . But I do not know how to do that and also how to resize JLabel dynamically.
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

I solved this problem already!

Now I calculate size of JLabel for new text as so:

tempLabel.getPreferredSize() always returns preferred size of JLabel that acommodates the whole text and nothing more.

Thanks again!
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It also depends on the LayoutManager you are using. Some of these determine the size by themselves,
no matter what preferred size there is.

Can't you simply set the text in your label, and then call label.revalidate() and label.repaint()?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Can't you simply set the text in your label, and then call label.revalidate() and label.repaint()?


Not needed. From JLabel.java
 
reply
    Bookmark Topic Watch Topic
  • New Topic