Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JLabel versus JOptionPane multiline question

 
Ronald Wouters
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I know a JLabel can't handle "\n" to separate multiple lines. However, using a JOptionPane seems to work. I mean:

does NOT work, but

does work as you can see (hopefully) in the following screenshot:


Can anybody tell me why ?
Regards, Ronald.
 
Pawel Poltorak
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ronald,

You may want to check BasicOptionPaneUI.addMessageComponents. This method, if message is String, calls itself recursively with substring from 0 to position of first new line character. This way you have multi line message.

Hope this helps,
Pawel

PS. You can break text on JLabel if you make it in html and use <br>.
 
Ronald Wouters
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pawel,

thanks for your reply. I found the code you mentioned in the src.zip of the JDK. That clears up a lot. I understand the difference now. I did now about using html in JLabels already. I had just hoped to have one single way of using multiple lines. Guess Swing is not that simple.

Regards,
Ronald.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic