Java复选框列表选择侦听器

时间:2016-05-20 15:30:07

标签: java swing jlist jcheckbox

我有制作复选框列表的代码。但是,如果我单击一行中的任何位置,则复选框选择会更改。我希望只有在单击复选框本身时才会发生这种情况。如何纠正这个听众是“准确的”?

这是SSCCE

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class CheckBoxList extends JList
{
    public CheckBoxList()
    {
        super();

        setModel(new DefaultListModel<>());
        setCellRenderer(new CheckboxCellRenderer());

        addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent mouseEvent)
            {
                int index = locationToIndex(mouseEvent.getPoint());

                if (index != -1)
                {
                    Object object = getModel().getElementAt(index);

                    if (object instanceof JCheckBox)
                    {
                        JCheckBox checkbox = (JCheckBox) object;

                        checkbox.setSelected(!checkbox.isSelected());
                        repaint();
                    }
                }
            }
        });

        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    public static void main(String[] arguments)
    {
        JFrame frame = new JFrame();
        DefaultListModel<String> listModel = new DefaultListModel<>();
        listModel.addElement("Element 1");
        listModel.addElement("Element 2");
        JList<String> list = new JList<>();
        list.setCellRenderer(new CheckboxListCellRenderer<>());
        list.setModel(listModel);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(list);
        frame.pack();
        frame.setVisible(true);
    }
}

class CheckboxCellRenderer extends DefaultListCellRenderer
{
    private static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);

    public Component getListCellRendererComponent(JList list, Object value, int index,
                                                  boolean isSelected, boolean cellHasFocus)
    {
        if (value instanceof CheckBoxListEntry)
        {
            CheckBoxListEntry checkbox = (CheckBoxListEntry) value;
            checkbox.setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
            if (checkbox.isRed())
            {
                checkbox.setForeground(Color.red);
            } else
            {
                checkbox.setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
            }
            checkbox.setEnabled(isEnabled());
            checkbox.setFont(getFont());
            checkbox.setFocusPainted(false);
            checkbox.setBorderPainted(true);
            checkbox.setBorder(isSelected ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);

            return checkbox;
        } else
        {
            return super.getListCellRendererComponent(list, value.getClass().getName(), index,
                    isSelected, cellHasFocus);
        }
    }
}

class CheckBoxListEntry extends JCheckBox
{
    public CheckBoxListEntry(Object itemValue, boolean selected)
    {
        super(itemValue == null ? "" : "" + itemValue, selected);
    }

    public boolean isSelected()
    {
        return super.isSelected();
    }

    public void setSelected(boolean selected)
    {
        super.setSelected(selected);
    }

    boolean isRed()
    {
        return false;
    }
}

0 个答案:

没有答案