Using this code snippet, we can create scrollable List box in Java Swing
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(22, 24, 265, 155);
panel_2.add(scrollPane);
// Create DefualtListModel and JList
private DefaultListModel roomListModel = new DefaultListModel();
private JList roomsJList;
// Add data to the JList using DefaultListModel
roomsJList = new JList(roomListModel);
scrollPane.setViewportView(roomsJList);
//Setting auto scroll to true
roomsJList.setAutoscrolls(true);
// Adding value changed Action listener to List items.
roomsJList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
if(roomsJList.getSelectedValue() !=null){
// Add code here which will run on List Selection
}
}
});