How to add ScrollBar to JList in Java?


Let us first set a JList and add items −

List<String> myList = new ArrayList<>(10);
for (int index = 0; index < 20; index++) {
   myList.add("List Item " + index);
}

Now, we will set the above list to a new list −

final JList<String> list = new JList<String>(myList.toArray(new String[myList.size()]));

Now, set the ScrollBar to JList −

JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(list);
list.setLayoutOrientation(JList.VERTICAL);

The following is an example to add ScrollBar to JList −

Example

package my;
import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class SwingDemo {
   public static void main(String[] args) {
      JPanel panel = new JPanel(new BorderLayout());
      List<String> myList = new ArrayList<>(10);
      for (int index = 0; index < 20; index++) {
         myList.add("List Item " + index);
      }
      final JList<String> list = new JList<String>(myList.toArray(new String[myList.size()]));
      JScrollPane scrollPane = new JScrollPane();
      scrollPane.setViewportView(list);
      list.setLayoutOrientation(JList.VERTICAL);
      panel.add(scrollPane);
      JFrame frame = new JFrame("Demo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(panel);
      frame.setSize(500, 250);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
}

This will produce the following output −


Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements