Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple lines on JLabel

 
Ryan Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my app, I want a JLabel to display text; however, there is a lot of text, and it overflows the one-line area, out of the component's designated area.

I looked, but there doesn't seem to be a method to set multi-line text for JLabel.

Is there another class I should be using, or a method that would accomplish multi-lining?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use HTML.

 
Ryan Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WHAT?!?!

Java supports internal HTML???
 
Ryan Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is craziness!
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ryan Smith:
This is craziness!


 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A common technique for multi-line labels is to use a JTextArea.

The Rowan project contains a method that looks like this, which is pretty much identical to code I've written in the past and code I believe I've seen in JGoodies examples as well:



The choice of whether to leave it focusable may depend on context. I usually prefer to allow focus so users can copy the text, but sometimes that will confuse people navigating in dialog boxes who wonder where the cursor went (focusable but non-editable JTextAreas don't display a cursor, at least in Windows look and feels, but it actually still exists).

Another project called L2FProd Common Components (which has some really neat components!) sports this handy method:



You might want to steal features from either one to craft the perfect funciton for your needs.

One bit of caution if you use word wrap: I've noticed that JTextArea's with word wrap on are very greedy about space if you don't put them inside a scroll pane. If you allow them to grow by enlarging the window, they refuse to shrink back to their original size if you make the window smaller again! The easy workaround is to put them in a scroll pane and disable scrolling.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are excellent ideas David. I never thought of that before.

The easy workaround is to put them in a scroll pane and disable scrolling.

Wouldn't you still see the scroll bars though? This would make it look like a JTextArea and not a Label anymore it would seem.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DW: The easy workaround is to put them in a scroll pane and disable scrolling.

GB: Wouldn't you still see the scroll bars though? This would make it look like a JTextArea and not a Label anymore it would seem.

VERTICAL_SCROLLBAR_NEVER and HORIZONTAL_SCROLLBAR_NEVER prevent scroll bars from being displayed at all, and once the text area figures out its preferred size it won't change its mind. This resizing issue was something I randomly encountered once, and it might have been related to my particular choice of layout manager and layout constraints. So consider it noted for future reference: If your JTextAreas are greedy about space, scroll panes may help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic