Search code examples
javaswingjscrollpanejlistjoptionpane

How do I change the size of a JScrollPane in java


I have never used one of these before.I have only used normal JOptionPane boxes.This is what I have at the moment:

JOptionPane.showMessageDialog(null,new JScrollPane(new JList(uniqueWords.toArray())),"Unique Words",1);

where uniqueWords is an array list.The problem is the length of the dialog box is tiny.You can only see one line on screen at a time.How can I make it display more at a time. Also is it possible to get rid of these notes and what causes them

Note: Files.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

EDIT: I changed my code to this

JScrollPane jspane = new JScrollPane(new JList(uniqueWords.toArray()));
    Dimension size = new Dimension(1000,1000);
    jspane.setMinimumSize(size);
    JOptionPane.showMessageDialog(null,jspane,"Unique Words",1);

but it is still displaying the same dialog box


Solution

  • "How can I make it display more at a time"

    You can just override the getPreferredSize of the JScrollPane

    import java.awt.Dimension;
    import java.util.*;
    import javax.swing.*;
    
    public class Test3 {
    
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    List<String> list = new ArrayList<>();
                    for (int i = 0; i < 30; i++) {
                        list.add("Hello, World " + i);
                    }
                    JScrollPane pane = new JScrollPane(new JList(list.toArray())) {
                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(200, 250);
                        }
                    };
                    JOptionPane.showMessageDialog(null, pane);
                }
            });
        }
    }
    

    "Also is it possible to get rid of these notes and what causes them"

    You should do what it says and recompile with -XLint and it will show you details of the warnings