1

I'm trying to add a listener to each checkbox from the implementation here: How do I make a list with checkboxes in Java Swing?, but am not sure which interface to implement. Does anyone have any recommendations?

public class CheckBoxList extends JList {
    protected Border noFocusBorder =
            new EmptyBorder(1, 1, 1, 1);

    public CheckBoxList() {
        setCellRenderer(new CellRenderer());
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e){
                int index = locationToIndex(e.getPoint());
                if (index != -1) {
                    JCheckBox checkbox = (JCheckBox)
                            getModel().getElementAt(index);
                    checkbox.setSelected(
                            !checkbox.isSelected());
                    repaint();
                }
            }
        });
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }
    //adds checkboxes..
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void addCheckbox(JCheckBox checkBox) {
        ListModel currentList = this.getModel();
        JCheckBox[] newList = new JCheckBox[currentList.getSize() + 1];
        for (int i = 0; i < currentList.getSize(); i++) {
            newList[i] = (JCheckBox) currentList.getElementAt(i);
        }
        newList[newList.length - 1] = checkBox;
        setListData(newList);
    }
    @SuppressWarnings("rawtypes")
    protected class CellRenderer implements ListCellRenderer {
        public Component getListCellRendererComponent(
                @SuppressWarnings("rawtypes") JList list, Object value, int index,
                boolean isSelected, boolean cellHasFocus) {
            JCheckBox checkbox = (JCheckBox) value;
            checkbox.setBackground(isSelected ?
                    getSelectionBackground() : getBackground());
            checkbox.setForeground(isSelected ?
                    getSelectionForeground() : getForeground());
            checkbox.setEnabled(isEnabled());
            checkbox.setFont(getFont());
            checkbox.setFocusPainted(false);
            checkbox.setBorderPainted(true);
            checkbox.setBorder(isSelected ?
                    UIManager.getBorder(
                            "List.focusCellHighlightBorder") : noFocusBorder);
            return checkbox;
        }
    }
}
Community
  • 1
  • 1
NumenorForLife
  • 1,736
  • 8
  • 27
  • 55
  • Don't add components to a model, add data and let the views render decide how best to display it. When you change the state of data in the model, use the model to notify the view. This approach is highly optimised to reduce to the amount of repainting that is invoved to update the view...the short answer to your question is you don't want to – MadProgrammer Apr 29 '14 at 20:46

1 Answers1

4

As suggested in a comment by @MadProgrammer and shown here, here and here, you can use JTable for the view and store Boolean values in the corresponding TableModel.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045