Display Multiline Messages with JOptionPane in Java

Description

The following code shows how to display Multiline Messages with JOptionPane.

Example


   /*from w  w  w.jav a  2s.  co  m*/


import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

public class Main {

  public static void main(String[] a) {

    String msg = "<html>this is a really long message<br>this is a really long message this is a really long message this is a really long message this is a really long message this is a really long message this is a really long message";

    JOptionPane optionPane = new NarrowOptionPane();
    optionPane.setMessage(msg);
    optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
    JDialog dialog = optionPane.createDialog(null, "Width 100");
    dialog.setVisible(true);
  }
}

class NarrowOptionPane extends JOptionPane {

  NarrowOptionPane() {
  }

  public int getMaxCharactersPerLineCount() {
    return 100;
  }
}

The code above generates the following result.

Display Multiline Messages with JOptionPane in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer