覆盖在 getListCellRendererComponent 中切换 'isSelected' 的方法

Override method that toggles 'isSelected' in getListCellRendererComponent

我想制作自己的方法来控制组件何时 'isSelected'。

我有一个 JList 包含多个 JPanelJPanel extends ListCellRenderer<>的构造class。

显示 JList 组件之一(JPanel)是我使用的 selected;

@Override
public Component getListCellRendererComponent(..., boolean isSelected, ...) {
    if(isSelected){
        setBackground(list.getSelectionBackground());
        setForeground(list.getSelectionForeground());
    } else {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
    }
    return this;
}

我想要一种方法来保留 selected 项目 'selected' 尽管我选择 select 另一个。我知道这可以通过按住 CTRL 来完成,但是 .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 并不能完全解决问题。我宁愿通过单击它们来 select 多个,并通过单击它们来删除 select。

为此,我使用了 ListSelectionMode,但我找不到方法。

完成上述操作后,我想实现一种方法,当在特定区域(而不是预设的整个组件)中单击时,仅 selects 是列表中的一个组件。我做了这个方法,如果点击了正确的区域,returns 为真,否则为假。但是由于我无法弄清楚如何覆盖使组件 'isSelected' 发生的鼠标事件,所以这很棘手。

这是我想覆盖 'isSelected' 方法的方法的代码;

this.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent evt) {
            if(ActionHandler.mouseClickedPrebuild(evt.getPoint())){
                //This code runs if that special place is clicked!
                //So now the component should be 'isSelected' or
                //deselected if it already was 'isSelected'.
            }
        }
    });

这段代码在我的 JList

的构造函数中

mouseClickedPrebuild方法;

public static boolean mouseClickedPrebuild(Point point) {
        int index = theJList.locationToIndex(point);
        Rectangle bounds = theJList.getCellBounds(index,index);
        Point p = bounds.getLocation();
        return ( ... long list of greater than & less than ...);
        //This gives the certain area which is accepted to return true

我解决了这个问题!

所以我通过 运行 这条线显示了我的观点;

// UI Class   JScrollPane               Custom JList
UIConstructor.listview.setViewportView(new ListView( -insert ArrayList here- ));

这是我的 ListView。我用来解决问题的自定义 DefaultListSelectionModel 由@FuryComptuers 发布在此处;

JList - deselect when clicking an already selected item

我不得不对代码进行一些更改,因为 selectionModel 中的两个方法将 运行 在我的鼠标事件之前。我静态地保存了变量,所以我没有在 setSelectionInterval 中 运行 宁代码,而是在我的 mousePressed 中做的。

然后我可以添加布尔值 isSelected,如果单击特定列表元素中的窗帘区域,则 returns 为真。

public class ListViewd extends JList {

static boolean isSelected;
static Point point;

static boolean gS = false;
static int in0;
static int in1;

@Override
public Dimension getPreferredScrollableViewportSize() {
    Dimension size = super.getPreferredScrollableViewportSize();
    size.setSize(new Dimension(0,0));
    return size;
}

public ListView(ArrayList<System> items) {

    DefaultListModel<System> list = new DefaultListModel<System>();

    for (System item : items) {
        list.addElement(item);
    }

    this.setSelectionModel(new DefaultListSelectionModel() {

        boolean gestureStarted = false;

        @Override
        public void setSelectionInterval(int index0, int index1) {
            gS = gestureStarted;
            in0 = index0;
            in1 = index1;

            gestureStarted = true;
        }

        @Override
        public void setValueIsAdjusting(boolean isAdjusting) {
            if (!isAdjusting) {
                gestureStarted = false;
            }
        }

    });

    ListSelectionModel selectionModel = this.getSelectionModel();

    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            point = e.getPoint();
            isSelected = ActionHandler.mouseClickedPrebuild(point);

            if(!gS && isSelected){
                if (isSelectedIndex(in0)) {
                    selectionModel.removeSelectionInterval(in0, in1);
                } else {
                    selectionModel.addSelectionInterval(in0, in1);
                }
            }
        }
    });

    setModel(list);
    setCellRenderer(new ListModelPrebuild());
}