I'm not well-versed in HTML/CSS, so please bear with my limited knowledge. I'm currently developing a GUI where I need to display a message dialog in case of an error. The error message could be either short or lengthy. I've implemented the following code, inspired by this solution.
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class TestOptionDialog {
public static void main(String[] args) {
String msgLong = "This is a really long messageThis is a really long messageThis is a really long messageThis is a really long messageThis is a really long messageThis is a really long messageThis is a really long messag";
String msgShort = "This is a short message";
int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
JOptionPane.showMessageDialog(new JFrame(),
"<html><body><p style='width:100%;max-width:" + 0.6 * screenWidth + "px'>" + msgLong + "</p></body></html>",
"Error", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(new JFrame(),
"<html><body><p style='width:100%;max-width:" + 0.6 * screenWidth + "px'>" + msgShort + "</p></body></html>",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
I'm facing an issue where setting the width variable to a specific pixel number only results in long messages being wrapped correctly, while short messages cause the dialog to be much larger than the text. On the other hand, setting the max-width argument only allows short messages to be displayed with the correct size, but long messages are not wrapped as desired. How can I achieve both?
UPDATE: I tried Marius' solution, which works quite well. However, I'm now encountering an issue where even though the calculated text width surpasses the threshold (e.g., 60% of screenWidth), the dialog doesn't break. Can anyone shed light on what might be causing this? See the code snippet and output below:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class TestOptionDialog {
public static void main(String[] args) {
// 1512 px
String message = "message message message message message message message message message message message message message message message message message message message message message message message message message message message ";
// 1232 px
String message1 = "message message message message message message message message message message message message message message message message message message message message message message ";
// 1064 px
String message2 = "message message message message message message message message message message message message message message message message message message message ";
// Break line at:
int threshold = 1152;
JOptionPane.showMessageDialog(new JFrame(),getFormattedErrorMessage(message, threshold),
"Error", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(new JFrame(),getFormattedErrorMessage(message1, threshold),
"Error", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(new JFrame(),getFormattedErrorMessage(message2, threshold),
"Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
private static String getFormattedErrorMessage(String message, int maxDialogWidth) {
String string;
JLabel label = new JLabel(message);
if (label.getPreferredSize().width > maxDialogWidth) {
string = "<html><body><p style='width:" + maxDialogWidth + "px;'>" + message + "</p></body></html>";
} else {
string = "<html><body><p>" + message + "</p></body></html>";
}
return string;
}
}
1064 px: https://i.sstatic.net/JIFBQ.png 1232 px: https://i.sstatic.net/vjnzR.png 1512 px: https://i.sstatic.net/6wYZn.png