A JList Java component with checkable items.
/** * Very simple example that creates a list of checkable items. The user can check/uncheck the items * he wants. When the window is closed, the state of each item is printed on the standard output. * * @author Bart Jourquin */importjava.awt.Dimension;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjavax.swing.DefaultCheckListModel;
importjavax.swing.JCheckList;
importjavax.swing.JFrame;
importjavax.swing.JScrollPane;
publicclassExample {
/** Constructor. */publicExample() {
JFrameframe = newJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a list containing CheckboxListItem'sfinalDefaultCheckListModel<String> myModel = newDefaultCheckListModel<String>();
JCheckList<String> myCheckList = newJCheckList<>(myModel);
// Fill itfor (inti = 0; i < 10; i++) {
myModel.addItem("Item " + i);
}
// Just print the state of each item at closing timeframe.addWindowListener(
newWindowAdapter() {
@OverridepublicvoidwindowClosing(WindowEvente) {
for (inti = 0; i < myModel.getSize(); i++) {
if (myModel.isChecked(i)) {
System.out.println("Item " + i + " is checked");
} else {
System.out.println("Item " + i + " is unchecked");
}
}
e.getWindow().dispose();
}
});
frame.getContentPane().add(newJScrollPane(myCheckList));
frame.setSize(newDimension(150, 250));
frame.setVisible(true);
}
publicstaticvoidmain(String[] args) {
newExample();
}
}