Swing dialog displays long string differently on Mac and Windows
I need to have a long descriptive dialog in a part of my program, and it displays differently on Mac and Windows. On a mac file, the word seems to wrap the text and break it into 3 or 4 lines, but on a PC it just creates a very long dialog. Here is the code that shows my problems:
public class Test extends JFrame{
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Test extends JFrame{
private String suggestion = "eee eee eeee eeee eeeerr rrrrr rrrrrrrr rrrrrr " +
"rrrrrr rrrrrrrrr rrrrrr rrrrr tttttt ttttttttttt ttttt tttttttttt ttt" +
" tttt tttttt ttttttttttt reroew uewurkpe jwrkl;ejr kejk ejrk;jewr;jeklr " +
"jk jre;wlj;ewjr;ej lejrlkejlkejlkjerl ejlrj kleklr jekl jlek " +
"rjklejrklejrklekl ";
public void showDialog()
{
JOptionPane.showMessageDialog(this,
suggestion,
"title",
JOptionPane.INFORMATION_MESSAGE,
null);
}
public static void main(String [] args)
{
Test test = new Test();
test.showDialog();
}
}
when i run this on windows it just created one huge line of text in a long dialog, but on mac it generates multiple lines with appropriately sized dialom.
source to share
The JOptionPane component has a read-only property (MaxCharactersPerLineCount) for the maximum number of characters per line. By default, this is Integer.MAX_VALUE. By subclassing JOptionPane, you can override this parameter. Changing this setting allows the component to wrap words when the message is really long.
source to share
If you'd rather control word wrapping yourself, pass an array of strings to showMessageDialog. Each line will be displayed on a separate line. It works on any platform.
private String s1 = "eee eee eeee eeee eeeerr rrrrr rrrrrrrr rrrrrr";
private String s2 = "rrrrrr rrrrrrrrr rrrrrr rrrrr tttttt ttttttttttt ttttt tttttttttt ttt";
private String s3 = "tttt tttttt ttttttttttt reroew uewurkpe jwrkl;ejr kejk ejrk;jewr;jeklr";
private String s4 = "rjklejrklejrklekl";
private String s5 = "eee eee eeee eeee eeeerr rrrrr rrrrrrrr rrrrrr";
private String[] suggestion = new String[] {s1, s2, s3, s4, s5};
public void showDialog()
{
JOptionPane.showMessageDialog(this,
suggestion,
"title",
JOptionPane.INFORMATION_MESSAGE,
null);
}
source to share