How to get real size of the JLable with wrapped text
Hello all,
I have a JLable, which should display error message or some tips to the user. Text can have different length. Something like this:
jlblText.setText("<html><body>Error. Please do this and that. </body></html>")
view.pack();
The lable is on the top of the JDialog.
If user typed wrong data, an error message will be displayed in the lable. If the text is too long, jlable will be wrapped. JDialog should also adjust it's size, so I use pack().
In this case nothing happens. I wanted to get the height of the component and to increase the size of the JDialog, but
System.out.println("height = "+view.jlblText().getHeight());
returns the same result all time. No matter whether the lable contains 20 lines or just some words.
Is it possible to automatically adjust the size of the JDialog, if the size of the JLable is changed?
In this case I can't use JScrollPane.
Thanks in advance.
[1029 byte] By [
flexeda] at [2007-11-27 10:36:32]

# 3
> Now, about that SSCCE....
Thanks for your answer petes.
I didn't understand what SSCCE for. Clicking on some links I get internal server error.
It there some way to get the height of the component using standard Swing API?
# 5
Have you ever used a ComponentListener?
http://72.5.124.55/j2se/1.5.0/docs/api/java/awt/event/ComponentListener.html
It's componentResized() method should tell you when the JLabel has changed size, so you can adjust the size of the JDialog accordingly.
# 9
Thanks all for your answers.
Component resize could be interesting idea.
> pack() should do the trick.
I am wonder, why the pack after the setText method doesn't do the trick.
>
> Maybe you need to wrap it in a
> SwingUtilities.invokeLater(...)_ to make sure the
> HTML text in the label has been properly rendered.
Can you explain detailled please. Should I make my view Runnable and invoke it later, or should I put something other into the invokeLater method?
# 11
component listener solved the problem.
> Try something like:
> > label.setText(....);
>
> SwingUtilities.invokeLater(new Runnable()
> {
> public void run()
> {
> frame.pack();
> }
> });
>
This construction didn't do the trick. I tryed to invoke this.pack() of my JDialog in a run method.
I am still wonder, why only the component listener get the right (actual) value, and the getHeight() method called after the setText method returns the old value.