Java Swing JList
Chapter:
Swing
Last Updated:
02-10-2016 16:55:52 UTC
Program:
/* ............... START ............... */
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.SwingUtilities;
public class JavaSwingJList extends JFrame {
private JList<String> countryList;
public JavaSwingJList() {
DefaultListModel<String> listModel = new DefaultListModel<>();
listModel.addElement("C#");
listModel.addElement("Java");
listModel.addElement("Asp.net");
listModel.addElement("PHP");
listModel.addElement("Phython");
countryList = new JList<>(listModel);
add(countryList);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("JList Example");
this.setSize(200, 200);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JavaSwingJList();
}
});
}
}
/* ............... END ............... */
Output
Notes:
-
The class JList is a component which displays a list of objects and allows the user to select one or more items. A separate model, ListModel, maintains the contents of the list.
- JList() - Constructs a JList with an empty, read-only, model.
- JList(ListModel dataModel) - Constructs a JList that displays elements from the specified, non-null, model.
- JList(Object[] listData) - Constructs a JList that displays the elements in the specified array.
- JList(Vector<?> listData) - Constructs a JList that displays the elements in the specified Vector.
Tags
Swing JList, Java, Swing