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]
# 1

Note to all: this double post was done at my request. The original post can be found here:

http://forum.java.sun.com/thread.jspa?threadID=5194990&messageID=9768105#9768105

petes1234a at 2007-7-28 18:41:52 > top of Java-index,Desktop,Core GUI APIs...
# 2

Now, about that SSCCE....

petes1234a at 2007-7-28 18:41:52 > top of Java-index,Desktop,Core GUI APIs...
# 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?

flexeda at 2007-7-28 18:41:52 > top of Java-index,Desktop,Core GUI APIs...
# 4

our clicking on links has been disabled. You have to copy and paste. What SSCCE is is a short program that demonstrates your problem, but does not contain any extraneous unnecessary code. Also, it has to compile on its own without any additional files. It allows us to see your problem first hand and play with it directly.

petes1234a at 2007-7-28 18:41:52 > top of Java-index,Desktop,Core GUI APIs...
# 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.

CaptainMorgan08a at 2007-7-28 18:41:52 > top of Java-index,Desktop,Core GUI APIs...
# 6

what about calling pack() again or revalidate()?

(caveat: I'm throwing out ideas, but I'm no expert in this area)

petes1234a at 2007-7-28 18:41:52 > top of Java-index,Desktop,Core GUI APIs...
# 7

> what about calling pack() again or revalidate()?

That should work too, but I think that revalidate() is called through pack() (not sure, though).

CaptainMorgan08a at 2007-7-28 18:41:52 > top of Java-index,Desktop,Core GUI APIs...
# 8

pack() should 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.

camickra at 2007-7-28 18:41:52 > top of Java-index,Desktop,Core GUI APIs...
# 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?

flexeda at 2007-7-28 18:41:52 > top of Java-index,Desktop,Core GUI APIs...
# 10

Try something like:

label.setText(....);

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

frame.pack();

}

});

camickra at 2007-7-28 18:41:52 > top of Java-index,Desktop,Core GUI APIs...
# 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.

flexeda at 2007-7-28 18:41:52 > top of Java-index,Desktop,Core GUI APIs...