Make JList multi-selectable in Java
Description
The following code shows how to make JList multi-selectable.
Example
import java.awt.BorderLayout;
// ww w . j a v a2 s . c om
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Sizing Samples");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultListModel model = new DefaultListModel();
model.ensureCapacity(100);
for (int i = 0; i < 100; i++) {
model.addElement(Integer.toString(i));
}
JList jlist2 = new JList(model);
jlist2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
JScrollPane scrollPane2 = new JScrollPane(jlist2);
frame.add(scrollPane2, BorderLayout.CENTER);
frame.setSize(300, 350);
frame.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »