R
Rhino
I'm posting this in case anyone is trying to figure out how to put newline
characters in messages that they put in ResourceBundle message files in
their i18n (internationalization) efforts. I had the same
question the other day but couldn't find anything in Google or the Java
Tutorial that discussed this.
Two approaches that didn't work:
- embedding \n (or \\n) directly in the message text e.g.
msg014 = Failed to get database connection. \n\nError: {1}
- embedding the Unicode character for newline directly in the message text
e.g.
msg014 = Failed to get database connection. \u2424 Error: {1}
The approach I found which does work is to pass a newline to the message
just the way you would any other variable in the message. Here is the
relevant code fragment:
Connection dbConnection = null;
try {
dbConnection = DriverManager.getConnection(strURL, strUserName,
strPassword);
}
catch (SQLException sql_excp) {
Object[] msgArgs = { "\n", sql_excp }; //first variable is a newline
character, second variable is an exception message
msgFmt.applyPattern(locMsg.getString("msg016"));
String msg016 = msgFmt.format(msgArgs);
JOptionPane.showMessageDialog(this, msg016, null,
JOptionPane.ERROR_MESSAGE);
return(Result);
}
This is the message itself:
msg016 = Failed to get database connection. {0}{0}Error: {1}
Note that you can repeat the newline variable as many times as you like
within the message.
I assume that you could put other special characters, like tabs, into a
message in the same way but I haven't actually tried that yet.
characters in messages that they put in ResourceBundle message files in
their i18n (internationalization) efforts. I had the same
question the other day but couldn't find anything in Google or the Java
Tutorial that discussed this.
Two approaches that didn't work:
- embedding \n (or \\n) directly in the message text e.g.
msg014 = Failed to get database connection. \n\nError: {1}
- embedding the Unicode character for newline directly in the message text
e.g.
msg014 = Failed to get database connection. \u2424 Error: {1}
The approach I found which does work is to pass a newline to the message
just the way you would any other variable in the message. Here is the
relevant code fragment:
Connection dbConnection = null;
try {
dbConnection = DriverManager.getConnection(strURL, strUserName,
strPassword);
}
catch (SQLException sql_excp) {
Object[] msgArgs = { "\n", sql_excp }; //first variable is a newline
character, second variable is an exception message
msgFmt.applyPattern(locMsg.getString("msg016"));
String msg016 = msgFmt.format(msgArgs);
JOptionPane.showMessageDialog(this, msg016, null,
JOptionPane.ERROR_MESSAGE);
return(Result);
}
This is the message itself:
msg016 = Failed to get database connection. {0}{0}Error: {1}
Note that you can repeat the newline variable as many times as you like
within the message.
I assume that you could put other special characters, like tabs, into a
message in the same way but I haven't actually tried that yet.