Hello Members,

Is it possible to have a Scrollbar for a JOptionPane as described below?

public class Hello {

            public static void main (String args [])

                      JOptionPane.showMessageDialog ("..............");


If you can show me some pseudocode or a sample program, I would be very grateful.

Thank you!!

Hello Ezzaral,

Thanks for the quick reply.

Is the method , setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS)
useful in any way to what I am attempting to do?

If not, I will just do a custom dialog as you suggested.

Thank you!

Edited 6 Years Ago by sciprog1: n/a

No, setHorizontalScrollBarPolicy() is a JScrollPane method, but your option pane doesn't have one.

You could wrap a customized JDialog pretty easily like this

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ScrollableDialog{
    JDialog dialog;
    JTextArea textArea;
    int dialogResult = JOptionPane.CANCEL_OPTION;
    public ScrollableDialog(Frame owner, String text){
        // create a modal dialog that will block until hidden
        dialog = new JDialog(owner, true);

        dialog.setLayout(new BorderLayout());

        textArea = new JTextArea(text);
        JScrollPane scrollpane = new JScrollPane(textArea);

        JButton btnOk = new JButton("Ok");
        btnOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // This will "close" the modal dialog and allow program
                // execution to continue.
                dialogResult = JOptionPane.OK_OPTION;
    public int showMessage(){
        // show the dialog - this will block until setVisible(false) occurs
        // return whatever data is required
        return dialogResult;
    public static void main(String[] args) {
        String bigText = "When in the Course of human events, " +
                "it becomes necessary for one people" +
                " to dissolve the political bands which have " +
                "connected them with another, and to" +
                " assume among the powers of the earth, the " +
                "separate and equal station to which " +
                "the Laws of Nature and of Nature's God entitle" +
                " them, a decent respect to the " +
                "opinions of mankind requires that they should" +
                " declare the causes which impel them" +
                " to the separation.";

        ScrollableDialog dialog = new ScrollableDialog(null,bigText);
        int userChoice = dialog.showMessage();
        System.out.println("User chose: "+(userChoice==JOptionPane.OK_OPTION ? "Ok" : "Cancel"));


Hello Ezzaral,

Thanks for the explanation and the example!!


This article has been dead for over six months. Start a new discussion instead.