aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Long String in a JDialog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Long String in a JDialog" Watch "Long String in a JDialog" New topic
Author

Long String in a JDialog

Meshulam Silk
Greenhorn

Joined: Feb 12, 2012
Posts: 22
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

Joined: May 03, 2008
Posts: 4664
    
    5

To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19794
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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


SCJP
Visit my download page
Meshulam Silk
Greenhorn

Joined: Feb 12, 2012
Posts: 22
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

Joined: Jun 09, 2003
Posts: 4632
labels support html, just change \n's to br's (wrapped in tags)
Meshulam Silk
Greenhorn

Joined: Feb 12, 2012
Posts: 22
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

Joined: Jul 26, 2007
Posts: 10535
    
    9

Use a non editable JTextArea to display the text (with line wrap and wrap style word)
Then add the JTextArea to the JDialog


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Meshulam Silk
Greenhorn

Joined: Feb 12, 2012
Posts: 22
I'll try that out. Thanks!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Feb 12, 2012
Posts: 22
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

Joined: Feb 12, 2012
Posts: 22
Yup the center tags worked perfectly!

Before:

After:


It's a tiny change but looks so much better.
Thanks everyone!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Long String in a JDialog