Text Wrap in Joptionpane

Text wrap in JOptionPane?

A JOptionPane will use a JLabel to display text by default. A label will format HTML. Set the maximum width in CSS.

JOptionPane.showMessageDialog(
this,
"<html><body><p style='width: 200px;'>"+exp.getMessage()+"</p></body></html>",
"Error",
JOptionPane.ERROR_MESSAGE);

More generally, see How to Use HTML in Swing Components, as well as this simple example of using HTML in JLabel.


JOptionPane with text wrapping using custom Swing component

Exmaple

import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class OptionPaneUtilities {

public static void main(String[] args) {
OptionPaneUtilities.showMessage(null, "I sometimes I think we forget just how flexible the JOptionPane API is and what it can do. A little effort could go a long way");
}

public static void showMessage(Component comp, String message) {
JTextArea ta = new JTextArea(message, 1, 20);
ta.setWrapStyleWord(true);
ta.setLineWrap(true);
ta.setOpaque(false);
ta.setBorder(null);
ta.setEditable(false);
ta.setFocusable(false);
JOptionPane.showMessageDialog(comp, ta);
}

}

Now, obviously, this is just a really simple example, you're going to have to spend some time thinking about how best to apply this concept to your own API so it meets your needs and provides you with the greatest amount of flexibility.

You could make use of a JEditorPane instead and get both plain text and HTML to work with out to much more effort

Is there a word wrap property for JLabel?

Should work if you wrap the text in <html>...</html>

UPDATE:
You should probably set maximum size, too, then.

Displaying all text in one JOptionPane instead of multiple, line by line

You could try using a StringBuilder to create a single large string, then display this StringBuilder like so:

        Scanner inFile = null; 
StringBuilder builder = new StringBuilder();

try {
inFile = new Scanner(new FileReader(curDir + "/scripts/sysinfo.txt"));
} catch (FileNotFoundException ex) {
Logger.getLogger(DropDownTest.class.getName()).log(Level.SEVERE, null, ex);
}

while(inFile.hasNextLine()){
String line = inFile.nextLine();
builder.append(line);
builder.append("\n"); // add this for newlines
}

JOptionPane.showMessageDialog(null, builder, "System Information", JOptionPane.INFORMATION_MESSAGE);

Enable text highlighting in swing message-box

try this

 JTextArea textarea= new JTextArea("add your message here");
textarea.setEditable(true);
JOptionPane.showMessageDialog(null, textarea, "Error", JOptionPane.ERROR_MESSAGE);

JDialog doesn't size correctly with wrapped JTextArea

If you run the pack command on the dialog (a function in the Window class) it will resize based on subcomponents. For your case you will have to rewrite without using the showMessageDialog() to get the resize to work (so make the dialog first, add the text, pack, then show it)

Dialog b = new Dialog();
// add stuff
b.pack();

For my test code it worked perfectly to get the dialogs to be the right sizes

  1. Without pack() With Pack Command
  2. With pack() With Pack Command


Related Topics



Leave a reply



Submit