• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

JLabel versus JOptionPane multiline question

 
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.
 
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.
 
What a show! What atmosphere! What fun! What a tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!