0

For example, have a JLabel normally show its text when the text is short enough:

Hello world!

But automatically break it with "..." when it gets too long:

Hello ladies and gent...

Is there any way to do this when you use MigLayout? In other layouts this is the default behavior but appearantly not in MigLayout...

enter image description here

stommestack
  • 2,352
  • 5
  • 22
  • 38

2 Answers2

2

Not entirely sure that I understand what you mean by "break": I'm assuming you want the ellipses to show up (to indicate that the label is longer) vs. simply cutting the text off.

  • the ellipses are showing when the label's size is smaller than the text requires
  • the label's minSize is about the same as its preferred
  • by default, MigLayout doesn't size a component below its min

So you have to explicitly allow the manager to size smaller than the min by overruling the component's min with a component constraint:

content.add(new JLabel("we are looooong and getting longer by the minute"), "wmin 0");

(might be possible on a column constraint as well, didn't check)

kleopatra
  • 49,346
  • 26
  • 88
  • 189
1

Place it inside <html> tags and use <br /> to add new line

Mylabel.setText("<html><div style=\"text-align: center;\">"+
                    "First Line <br />"+
                    "Second Line</html>"
                );

This would look like

                 ----------------------
                 |    First Line      |
                 |    Second Line     | 
                 ----------------------
Ankit Rustagi
  • 5,203
  • 10
  • 34
  • 65
  • 2
    Nope. I thought that from glancing at the title, but that is *not* what the OP wants. Also, don't use `
    ` when there is [CSS](http://stackoverflow.com/a/7861833/418556) to do the job better.
    – Andrew Thompson Nov 16 '13 at 12:52