Search code examples
javaswingjlistscrollpane

How to show horizontal AND vertical scrollbar for a JList?


Today I was playing around with JList's, and then I found a problem. I know that my fields don't have good names, but as I said, I was just playing around.

I wanted to create a horizontal and a vertical JScrollPane for my JList. Actually, it works, but there still is a problem.

I always have to scroll to the right in order to see the vertical scrollpane: my ScrollPanes my ScrollPanes

My Code:

   final JList list = new JList(GUI.strings.toArray());

   JScrollPane scrollPane = new JScrollPane(list);
   JScrollPane scrollPanex = new JScrollPane(scrollPane);

   scrollPanex.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
   scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

   getContentPane().add(scrollPanex, BorderLayout.CENTER);

Is there a way I can fix this so I can always see both scrollbars?


Solution

  • Just use one JScrollPane and then set both scroll bar polices of the single JScrollPane.

    final JList list = new JList(GUI.strings.toArray());
    JScrollPane scrollPane = new JScrollPane(list);
    
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    
    getContentPane().add(scrollPane, BorderLayout.CENTER);
    

    e.g.,

    import java.awt.Dimension;
    import java.util.Random;
    
    import javax.swing.DefaultListModel;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JViewport;
    import javax.swing.SwingUtilities;
    
    public class ScrollPaneFun {
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                Random rand = new Random();
                DefaultListModel<String> lModel = new DefaultListModel<>();
                JList<String> myList = new JList<>(lModel);
                myList.setVisibleRowCount(20);
    
                for (int i = 0; i < 50; i++) {
                   StringBuilder sb = new StringBuilder();
                   sb.append(String.valueOf(i) + ": ");
                   for (int j = 0; j < 50; j++) {
                      for (int k = 0; k < 3; k++) {
                         char c = (char) ('a' + rand.nextInt('z' - 'a' + 1));
                         sb.append(c);
                      }
                      sb.append(' ');
                   }
                   lModel.addElement(sb.toString());
                }
    
                JScrollPane scrollPane = new JScrollPane(myList);
                scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
                scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                JViewport viewport = scrollPane.getViewport();
                int w = 400;
                int h = viewport.getPreferredSize().height;
                Dimension preferredSize = new Dimension(w, h);
                viewport.setPreferredSize(preferredSize);
                JOptionPane.showMessageDialog(null, scrollPane);
             }
          });
       }
    }