JOptionPane and scroll function
Asked Answered
B

4

11

I want to JList a lot of results in a JOptionPane, however, I'm not sure how to add in a scroll function should there be too many results. How would I add a scroll bar to a JOptionPane? Any help would be great.

Thanks.

Blamed answered 4/12, 2011 at 11:39 Comment(0)
P
25

Here is an example using a JTextArea embedded in a JScrollPane:

JTextArea textArea = new JTextArea("Insert your Text here");
JScrollPane scrollPane = new JScrollPane(textArea);  
textArea.setLineWrap(true);  
textArea.setWrapStyleWord(true); 
scrollPane.setPreferredSize( new Dimension( 500, 500 ) );
JOptionPane.showMessageDialog(null, scrollPane, "dialog test with textarea",  
                                       JOptionPane.YES_NO_OPTION);
Palocz answered 4/12, 2011 at 11:51 Comment(8)
please change that to the JList :-)Dispensable
@AndrewThompson Agree a showMessageDialog is more fit for this situation.Palocz
@Dispensable Yes, a JList should do it as well. The example I provided here is only for guidance.Palocz
not your down_voter, +1 for an alternative maybe you posted an answer to this question, @Andrew Thompson hmmm maybe you are right, something important we missed here, connection .....pip - pip - pip, from OP's side is broken :-) , then I leave any my comment to your words...Dispensable
And you edited the code! :-) Heck, I'd up-vote if I'd not already done that. ;)Jus
@Dispensable "pip - pip - pip, from OP's side is broken" (chuckles) I may be getting old, but I can recall criticizing people on usenet for trying to 'bump' a post before 72 hours had passed. Seems you have to get an answer in within the first hour (sometimes the 1st 10 minutes) to be in the running around here. ;)Jus
you also need to set the preferred size on the scrollpane...I've edited the answerJoletta
Just be sure you use .setViewportView() instead of .add() on the JScrollPane or else the sub-controls may not show up.Ingulf
J
4

Put the objects in a JList or other such component, drop it into a JScrollPane, and put the JScrollPane into the JOptionPane.

Jus answered 4/12, 2011 at 11:45 Comment(0)
D
3

you can add any JComponent(s) to JOptionPane, including JScrollPane contains JList

Dispensable answered 4/12, 2011 at 11:46 Comment(0)
D
0

I had a similar need, a JOptionPane with a Scrolling TextArea that any of my classes across my application could write to. This was to provide the user with status and progress information. My approach was to make this a static class that I instantiated once and any class could call its update method to write to it. Below is the code and a small driver in the hopes it saves someone esle the time. This could be made not static, that just fit my needs.

package com.acme.view;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import com.acme.controller.MyController;
import com.acme.utils.NonModalMessage;

public class MyView {

    private JFrame frame;
    private int dialogNum = 0;
    private MyController myCntrlr;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        NonModalMessage.createMesgDialog();
        NonModalMessage.updateMessage("Acme Anvil Targeting Progress");

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MyView window = new MyView();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public MyView() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 250, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        myCntrlr = new MyController();

        JButton btnMybutton = new JButton("myButton");

        btnMybutton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                NonModalMessage.setMessageVisible();
                if(dialogNum > 0 && dialogNum % 10 == 0){
                    NonModalMessage.clearMessage();
                    NonModalMessage.updateMessage("Acme Anvil Targeting Progress");
                    myCntrlr.someMethod("Controller reports Roadrunner sighted. Message Number: ", dialogNum);
                }
                NonModalMessage.getMessage();

                NonModalMessage.updateMessage("Message number: " + Integer.toString(dialogNum));
                System.out.println("dialogNum: " + dialogNum);
                dialogNum++;
            }
        });

        frame.getContentPane().add(btnMybutton, BorderLayout.NORTH);
    }


}

package com.acme.controller;
import com.acme.utils.NonModalMessage;

public class MyController {

    public MyController(){

    }

    public void someMethod(String mystring, int myInt){
        NonModalMessage.updateMessage(mystring + " "+ myInt);
    }

}

package com.acme.utils;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

public class NonModalMessage {
    private static JTextArea textArea = null;
    private static JOptionPane oPane   = null;
    private static JDialog dialog     = null;
    private static JScrollPane myjsPane = null;
    public NonModalMessage(){}

    public static void createMesgDialog(){

        textArea = new JTextArea(); 
        textArea.setLineWrap(true);  
        textArea.setWrapStyleWord(true); 
        myjsPane = new JScrollPane(textArea);
        myjsPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        oPane = new JOptionPane();
        oPane.add(myjsPane);    

        //final JDialog dialog = pane.createDialog(myPane, "Progress Dialog");
        dialog = oPane.createDialog(null, "");
        dialog.setTitle("Progress Messages");
        dialog.setModal(false); 
        dialog.setSize(400, 250);
        dialog.setResizable(true);
        dialog.setAlwaysOnTop(true);
    }

    public static void setMessageVisible(){
        dialog.setVisible(true);
    }

    public static void updateMessage(String newMessage){
        String mytext = textArea.getText();
        if(mytext.isEmpty()){
            textArea.setText(newMessage);
        }
        else{
            textArea.setText(mytext + "\n" + newMessage);
        }

        oPane.setMessage( myjsPane );
    }
    public static String getMessage(){
        return textArea.getText();
    }

    public static void clearMessage(){
        textArea.setText("");
        oPane.setMessage( myjsPane );
    }

}
Deposition answered 9/1, 2015 at 22:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.