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!!
sciprog1

Recommended Answers

All 4 Replies

No, you would need to implement your own custom dialog for that.

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!
sciprog1

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);
        textArea.setLineWrap(true);
        
        JScrollPane scrollpane = new JScrollPane(textArea);

        dialog.add(scrollpane,BorderLayout.CENTER);
        
        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;
                dialog.setVisible(false);
                dialog.dispose();
            }
        });
        dialog.add(btnOk,BorderLayout.SOUTH);
        
        dialog.setSize(200,200);
    }
    
    public int showMessage(){
        // show the dialog - this will block until setVisible(false) occurs
        dialog.setVisible(true);
        // 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!!

Regards,
sciprog1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.