JDK-4104906 : JOptionPane should wordwrap message text
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.1.5,1.1.6,1.2.0,6
  • Priority: P5
  • Status: Open
  • Resolution: Unresolved
  • OS: generic,windows_nt
  • CPU: generic,x86
  • Submitted: 1998-01-19
  • Updated: 2022-09-14
Related Reports
Duplicate :  
Duplicate :  
Description
Name: diC59631			Date: 01/19/98


When displaying a message using JOptionPage, long
messages are all placed on 1 line, extending the
dialog potentially off the screen, depending on
the screen size.  The code should use some algorithm
to wrap messages, for example so that they are no
more than 2/3 screen width. (Consult Human factors)
(Review ID: 23491)
======================================================================

Name: gsC80088			Date: 02/23/99


1. Steps to reproduce the problem
----------------------------------
a. Compile and run the attached code
b. Enter a large number (~100) into the 
   textfield
c. Click on the create button
d. Notice how the JOption pane fills the
   height of the screen. The JScrollPane
   containing the JTextArea is not 
   displaying correctly and getting clipped
   at the top and bottom edges. The "OK" button
   is also not visible.

2. Source Code
-----------------------------------------

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;

public class Test extends JFrame implements ActionListener
{
	JTextField tf = new JTextField(10);
	JButton b = new JButton("Create");


	public static void main(String args[])
	{
		(new Test()).setVisible(true);
	}


	public Test()
	{
		getContentPane().setLayout(new FlowLayout());
		
		getContentPane().add(new JLabel("Number of lines in stack trace:"));

		getContentPane().add(tf);
		getContentPane().add(b);
		b.addActionListener(this);
		tf.setText("100");

		setSize(400,100);
	}


	public void actionPerformed(ActionEvent e)
	{
		showDlgBox(Integer.parseInt(tf.getText()));
	}


	/** 
	 *  show an error dialog box w/ stack trace w/ x # of lines
	 */
	void showDlgBox(int lines)
	{	
		foo(lines);
	}


	/**
	 * recurse to create x number of lines in stack trace 
	 */
	void foo(int lines)
	{
		if (lines == 0)
			showExceptionDialog(new Exception("testing"));
		else
			foo(lines-1);
	}


	/** 
	 *	dump stack trace to a textarea and pass the wrapper scrollpance
	 *  as the message paramater to showMessageDialg().
	 *  The textarea should grow as big as the screen allows and then
	 *  the scrollpane should add scrollbars if larger than screen.
	 *  However, the scrollpane gets clipped and the "OK" button is
	 *  not visible anymore.
	 */
	public static void showExceptionDialog(Exception e)
	{
		JTextArea ta = new JTextArea();
		ta.append(stackTraceToString(e));
		JOptionPane.showMessageDialog(new JFrame(), new JScrollPane(ta), "Exception", JOptionPane.ERROR_MESSAGE);
	}


    /**
     *  converts an exception's stack trace to a string
     */
    public static String stackTraceToString(Exception e)
    {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        return sw.toString();
    }

}

3. Error Messages
------------------
N/A

4. Trace Info
---------------
N/A

5. Additional Information
-------------------------
Running on JDK 1.1.6 
Swing 1.0.2
java -nojit
1024x768x32K
Swing Look and Feel
======================================================================

Name: gsC80088			Date: 02/23/99


Create a JOptionPane with a message containing a
JScrollPane (wrapping some other component). The
initial size will be very short. If you resize the
frame large enough (about twice the size of the
JTable in the example), it will show.

Run the following code with Swing 1.1beta2.

---------
import com.sun.java.swing.*;
import com.sun.java.swing.table.*;

public class OptionPaneBug
{

    private static class DummyModel extends AbstractTableModel {
        public int getColumnCount() { return 10; }
        public int getRowCount() { return 10; }
        public Object getValueAt(int row, int col)
            { return "("+row+","+col+")"; }
    }

    public static void main(String arg[]) {
        JOptionPane.showConfirmDialog(null,
            new JScrollPane(new JTable(new DummyModel())));
        JOptionPane.showConfirmDialog(null,
            new JScrollPane(new JTextArea(24,80)));
    }

}
======================================================================

Comments
EVALUATION I think it is too risky to change JLable to JTextArea see workaround section
10-02-2006

WORK AROUND public class MyTest { public static void main(String[] args) { String str = "Hello Leo!\n"; for (int i = 0; i < 5; i++) { str+=str; } JTextArea text = new JTextArea(str); JScrollPane scroll = new JScrollPane(text); scroll.setPreferredSize(new Dimension(100, 100)); JOptionPane.showMessageDialog(null, scroll); // with JTextArea } }
10-02-2006

EVALUATION Contribution-Forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?messageID=11276&forumID=1463
07-02-2006

EVALUATION [aim 3/12/98] I agree that the OptionPane should do more reasonable formatting for its message text. Since JLabel only supports single-lien text (currently), we'd need to use a TextArea to get more formatting.
24-09-2004