|
List with CheckBoxes.
Ignores the ListSelectionModel
Uses a specific model to the checked items: ListCheckModel
Example 1

| List<String> countries = Country.getCountries(); |
| |
| CheckList checkList = new CheckList(); |
| |
| ListCheckModel model = new DefaultListCheckModel(); |
| for (String country : countries) { |
| model.addElement(country); |
| } |
| |
| model.addCheck("Anguilla"); |
| model.addCheck("Algeria"); |
| |
| model.addLock("Algeria"); |
| model.addLock("Albania"); |
| |
| checkList.setModel(model); |
Example 2: changing the text renderer

| List<String> countries = Country.getCountries(); |
| |
| CheckList checkList = new CheckList(); |
| |
| ListCheckModel model = new DefaultListCheckModel(); |
| for (String country : countries) { |
| model.addElement(country); |
| } |
| |
| checkList.setModel(model); |
| |
| checkList.setCellRenderer(new CheckListRenderer() { |
| @Override |
| public String getText(Object value) { |
| String country = (String) value; |
| return country.toUpperCase(); |
| } |
| }); |
Example 3: defining a popup menu

| List<String> countries = Country.getCountries(); |
| |
| CheckList checkList = new CheckList(); |
| |
| ListCheckModel model = new DefaultListCheckModel(); |
| for (String country : countries) { |
| model.addElement(country); |
| } |
| |
| checkList.setModel(model); |
| |
| checkList.setPopupMenuBuilder(new PopupMenuBuilder<CheckList>() { |
| @Override |
| public JPopupMenu buildPopupMenu(final CheckList source) { |
| JPopupMenu menu = new JPopupMenu(); |
| |
| JMenuItem selectAll = new JMenuItem("select all"); |
| selectAll.addActionListener(new ActionListener() { |
| @Override |
| public void actionPerformed(ActionEvent e) { |
| source.getModel().checkAll(); |
| } |
| }); |
| |
| menu.add(selectAll); |
| return menu; |
| } |
| }); |
Example 4: adding listeners
| List<String> countries = Country.getCountries(); |
| |
| CheckList checkList = new CheckList(); |
| |
| ListCheckModel model = new DefaultListCheckModel(); |
| for (String country : countries) { |
| model.addElement(country); |
| } |
| |
| checkList.setModel(model); |
| |
| model.addListLockListener(new ListLockListener() { |
| @Override |
| public void removeLock(ListEvent event) { |
| System.out.println("Lock removed"); |
| } |
| |
| @Override |
| public void addLock(ListEvent event) { |
| System.out.println("Lock added"); |
| } |
| }); |
| |
| model.addListCheckListener(new ListCheckListener() { |
| @Override |
| public void removeCheck(ListEvent event) { |
| System.out.println("Check removed"); |
| } |
| |
| @Override |
| public void addCheck(ListEvent event) { |
| System.out.println("Check added"); |
| } |
| }); |
Example 5: working with locked and checked items
| List<String> countries = Country.getCountries(); |
| |
| CheckList checkList = new CheckList(); |
| |
| ListCheckModel model = new DefaultListCheckModel(); |
| for (String country : countries) { |
| model.addElement(country); |
| } |
| |
| checkList.setModel(model); |
| |
| model.addCheck("Canada", "Cape Verde"); |
| model.addLock("Canada", "Chile"); |
| |
| System.out.println("Checkeds: " + model.getChecksCount()); |
| for (Object checked : model.getCheckeds()) { |
| System.out.println(checked); |
| } |
| |
| System.out.println("Lockeds: " + model.getLocksCount()); |
| for (Object locked : model.getLockeds()) { |
| System.out.println(locked); |
| } |
|
|
|