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

Long String in a JDialog

 
Meshulam Silk
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there
I have a String which contains multiple lines ("\n") and I want to add it to a JDialog window. The thing is, the JDialog isn't wrapping around the text so some longer lines in the String will just run off the edge.
I there any way I can get the JDialog to resize itself so it fits the whole String into it?
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLabels don't break at new lines. Instead split the String on line breaks and add multiple JLabels. You'll have to set the layout manager correctly for them to be displayed below each other.
 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my impression has been that they do resize to fit the String. maybe the newlines are confusing it. be patient and someone will know for sure
 
Meshulam Silk
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually had a look into JLabels because of they wrap but I couldn't get them to show on top of each other, they just went side by side. Do you know which layout I should use? I tried a couple I found online but none did it.

Randall: I thought so too but no, they just run off. Also it's not THAT urgent.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
labels support html, just change \n's to br's (wrapped in tags)
 
Meshulam Silk
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw this too and tried it out. The problem with it is that the second line will align to the first line and won't be centered if it's a different length.
Also it trims the string so I can't put in spaces or tabs to nudge it. Is there a way to do this maybe?
 
Maneesh Godbole
Saloon Keeper
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a non editable JTextArea to display the text (with line wrap and wrap style word)
Then add the JTextArea to the JDialog
 
Meshulam Silk
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try that out. Thanks!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> The problem with it is that the second line will align to the first line and won't be centered if it's a different length.

use the center tag

 
Meshulam Silk
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That actually looks like exactly what I need.
Thanks man! I'll try this and using JTextArea and tell you which works best.
 
Meshulam Silk
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup the center tags worked perfectly!

Before:

After:


It's a tiny change but looks so much better.
Thanks everyone!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic