Here's the source code for a simple JOptionPane showMessageDialog example, where I use a JTextArea inside of a JScrollPane to show a long text message in the showMessageDialog:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* A Java class to demonstrate how to put a scrolling text area
* in a JOptionPane showMessageDialog dialog.
*
* Steps are simple - Just create a JTextArea, wrap it in a
* JScrollPane, and then add the JScrollPane to the showMessageDialog.
*/
public class ShowMessageDialogWithScrollpane implements Runnable
{
private JFrame frame = new JFrame("My JFrame Example");
private String longMessage = "Come and listen to a story about a man named Jed\n"
+ "A poor mountaineer, barely kept his family fed,\n"
+ "Then one day he was shootin at some food,\n"
+ "And up through the ground came a bubblin crude.\n"
+ "Oil that is, black gold, Texas tea.\n"
+ "Well the first thing you know ol Jed\'s a millionaire ...\n";
public static void main(String[] args)
{
ShowMessageDialogWithScrollpane example = new ShowMessageDialogWithScrollpane();
SwingUtilities.invokeLater(example);
}
public void run()
{
// start building a jframe
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(450, 300));
// add a button to the jframe
JButton button = new JButton("Click Me");
button.addActionListener(new ShowDialogListener());
frame.getContentPane().add(button);
// display the jframe
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
/**
* Our button listener. Show a scrolling text area in a
* JOptionPane showMessageDialog dialog.
*/
class ShowDialogListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// create a JTextArea
JTextArea textArea = new JTextArea(6, 25);
textArea.setText(longMessage);
textArea.setEditable(false);
// wrap a scrollpane around it
JScrollPane scrollPane = new JScrollPane(textArea);
// display them in a message dialog
JOptionPane.showMessageDialog(frame, scrollPane);
}
}
}
When this JOptionPane showMessageDialog dialog is displayed on Mac OS X 10.5, it looks like this:

The steps to creating this dialog with a scrolling text area is straightforward:
Of course there are many more things you can do to make this scrolling textarea look better, but I just wanted to share some code here to help you get started. I worked on a Java application a few years ago where our customer wanted us to show detailed messages to our users, so we adopted an approach like this to show scrolling text on our showMessageDialog dialogs.
Thanks Alvin, Great Post It
Thanks Alvin,
Great Post
It is going to come handy.
Post new comment