GridLayoutManager incorrectly sizes multi-line labels?

I've been banging my head over this for the past two days. I need to create a dialog with a relatively long warning text at the top, as well as some controls at the bottom. Well, what I usually do in this case is wrap the message in HTML tags, set it as text to a JLabel and set preferred label size to some fixed value (so that it'll wrap into several lines and the height would be computed automatically). The text would be localized, so I don't know in advance how many lines it'll occupy - so the label's height *has* to be dynamic. Unfortunately, it seems like GridLayoutManager doesn't compute the overall dialog's height correctly in this case - the dialog is smaller than required, so some controls (and sometimes even some label text if it's really long) get cut off and are invisible after the default .pack()/.setVisible(true) call.

If I change layout manager to GridBagLayout, the height of the dialog is computed properly and it shows all text and all components, as expected. Unfortunately, all other frames/dialogs in my application use IntelliJ GridLayoutManager, so this particular dialog looks somewhat inconsistent when managed by another layout manager :-(.

Any advice would be greatly appreciated.

2 comments
Comment actions Permalink

Don't use Label setPreferredSize()

Better way:

message.setText("<html><body style='width: 500px'>Lorem ipsum dolor sit amet, consectetur adipisicing elit," +
                "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br>" +
                "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris" +
                "nisi ut aliquip ex ea commodo consequat.</body></html>");



Attachment(s):
LongLabelDialog.java.zip
LongLabelDialog.form.zip
0
Comment actions Permalink

That seems to work much better, thanks a lot!

0

Please sign in to leave a comment.