• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: Wrapping JOption messages

 
Jacques Bosch
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way of automatically wrapping the text in a JOptionPane so that it doesn't have to be formatted manually to prevent a very wide message box that might fall off either end of the screen?
 
Alison Carter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jacques,
use newline character \n in your message to break it into shorter chunks that display on multiple lines:
JOptionPane display = new JOptionPane ("line 1 text \n line 2 text",
type, JOptionPane.DEFAULT_OPTION);
Regards,
Alison
 
Jacques Bosch
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alison. Thank you. But that is exactly what I don't want to do.
I wanted to know if there is a way, with out me having to write it first, of getting messages wrapped automatically if they are too long.
 
Alison Carter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Jacques,
other than using \n or a String[] I don't know of a way to wrap the messages. Maybe someone else knows?
Alison Carter
 
Jacques Bosch
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem.
I would have hoped that functionality was built into the JOption API.
 
Javini Javono
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Your point about the "cheap" implementation is a good one. After all,
how do we know where to put the "\n" character, since different operation
systems have different look and feel parameters.
1. The levels of warning is a standard part of the user interace (question mark,
explanation mark, and the like), and one would want to use these instead of
"re-inventing the wheel" by rolling your own alert box code. You can probably
get around this unknown by having your application only use one look and feel,
regardless of which operating system it is running on: metal (I think it's called,
and it comes with every later edition of the Java Run-time environment).
2. I'm not certain whether the font sizes used in the Metal look-and-feel, even
if that look and feel is running on a Mac or a Windows machine are identical.
They probably are very close. One idea would be to make each line 75% of the
width of the box.
3. If your text message is quite long, then you can either
3a. Look for a Sun supplied class that will assist you (I don't know of any myself,
though they certainly might exist),
3b. Roll your own dialog box by extending JDialog. But, again, I would not
do this unless your text message were overly long. I have forgotten if there
is any line limit on JOptionPane; but, you don't want to many lines for at some
point, for very small screens, the text won't show up properly.
Again, I agree with your concern; certainly these pop-up's should do all the
text wrapping, and come with automatic scroll bars.
Thanks,
Javini Javono
 
Jacques Bosch
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My feeling exactly!
I don't mind doing the manual wrapping for now, but it's always bothered me that a silly little thing like that takes away valuable development time from me.
Maybe, when this project is done, I'll have to write something that does it automatically. (Unless I can find something that is already done).
 
Alison Carter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
why not write a simple method that takes a single string and breaks it into an array of Strings each with a max length that you are happy with? Then use the array of Strings for the JOptionPane.
Just a thought.
Alison
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jacques,
Sun published a tutorial on how to do this last week. Unfortunately I deleted my copy, so I can't tell you the address to go to - take a look at their recent developer mailouts and you should find it.
From memory there is a protected field in JOptionPane which sets the maximum width of the text, and it is currently limitless. If you override JOptionPane you can then modify this value to set the wrap value.
Regards, Andrew
 
Xie Ruchang
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is here.
Best Regards
 
Jacques Bosch
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Alison, Andrew and Frankie. You're all beautiful!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic