To split a message in two or more lines in JOptionPane

try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://"+hostName.getText()+";" + "databaseName="+dbName.getText()+";user="+userName.getText()+";password="+password.getText()+";"; Connection con = DriverManager.getConnection(connectionUrl); if(con!=null){JOptionPane.showMessageDialog(this, "Connection Established");} } catch (SQLException e) { JOptionPane.showMessageDialog(this, e); //System.out.println("SQL Exception: "+ e.toString()); } catch (ClassNotFoundException cE) { //System.out.println("Class Not Found Exception: "+ cE.toString()); JOptionPane.showMessageDialog(this, cE.toString()); } 

When an error occurs, a long JOptionPane message box is displayed, which is larger than the width of the computer screen. How to split e.toString () into two or more parts.

+6
source share
4 answers

enter image description here

 import java.awt.*; import javax.swing.*; class FixedWidthLabel { public static void main(String[] args) { Runnable r = new Runnable() { public void run() { String pt1 = "<html><body width='"; String pt2 = "'><h1>Label Width</h1>" + "<p>Many Swing components support HTML 3.2 &amp;" + " (simple) CSS. By setting a body width we can cause the " + " component to find the natural height needed to display" + " the component.<br><br>" + "<p>The body width in this text is set to " + ""; String pt3 = " pixels."; JPanel p = new JPanel( new BorderLayout() ); int width = 175; String s = pt1 + width + pt2 + width + pt3 ; JOptionPane.showMessageDialog(null, s); } }; SwingUtilities.invokeLater(r); } } 
+21
source

You must use \n to split a line in different lines. Or you can:

Another way to solve this problem is to subclass the JOptionPane class and override getMaxCharactersPerLineCount so that it returns the number of characters you want to represent for at most one line of text.

β†’ http://ninopriore.com/2009/07/12/the-java-joptionpane-class/ (dead link, see archive copy ).

+3
source

Like Andrew Thomson , the following code allows you to load an HTML file from the project root directory and display it in JOptionPane . Note that you need to add the Maven dependency for Apache Commons IO . Also, using HTMLCompressor is a good idea if you want to read formatted HTML from a file without rendering disruption.

 import com.googlecode.htmlcompressor.compressor.HtmlCompressor; import org.apache.commons.io.FileUtils; import javax.swing.*; import java.io.File; import java.io.IOException; public class HTMLRenderingTest { public static void main(String[] arguments) throws IOException { String html = FileUtils.readFileToString(new File("document.html")); HtmlCompressor compressor = new HtmlCompressor(); html = compressor.compress(html); JOptionPane.showMessageDialog(null, html); } } 

This will allow you to better manage your HTML code than in Java Strings.

Remember to create a file called document.html with the following contents:

 <html> <body width='175'><h1>Label Width</h1> <p>Many Swing components support HTML 3.2 &amp; (simple) CSS. By setting a body width we can cause the component to find the natural height needed to display the component.<br><br> <p>The body width in this text is set to 175 pixels. 

Result:

+1
source

I set the character limit, then search for the last space character in this environment and write "\ n" there. (Or I force "\ n" if there is no space character). Like this:

 /** Force-inserts line breaks into an otherwise human-unfriendly long string. * */ private String breakLongString( String input, int charLimit ) { String output = "", rest = input; int i = 0; // validate. if ( rest.length() < charLimit ) { output = rest; } else if ( !rest.equals("") && (rest != null) ) // safety precaution { do { // search the next index of interest. i = rest.lastIndexOf(" ", charLimit) +1; if ( i == -1 ) i = charLimit; if ( i > rest.length() ) i = rest.length(); // break! output += rest.substring(0,i) +"\n"; rest = rest.substring(i); } while ( (rest.length() > charLimit) ); output += rest; } return output; } 

And I call it in the bracket (try) -catch:

 JOptionPane.showMessageDialog( null, "Could not create table 't_rennwagen'.\n\n" + breakLongString( stmt.getWarnings().toString(), 100 ), "SQL Error", JOptionPane.ERROR_MESSAGE ); 
0
source

All Articles