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 );
Woodrowshigeru
source share