java - JList - 设置允许的选择组合

标签 java selection combinations jlist

我有一个包含 8 个字符串项的 JList。客户希望有四种可能的选择组合。代码示例生成这样的列表。我用鼠标监听器以一种丑陋的方式部分地完成了这个任务。

import javax.swing.*;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.util.ArrayList;

class Selections {
    public static void main(String[] args) throws Exception {
        ArrayList<String> items = new ArrayList<String>();
        items.add("11");
        items.add("21");
        items.add("31");
        items.add("41");
        items.add("12");
        items.add("22");
        items.add("32");
        items.add("42");

        final ArrayList<String> combinationOne = new ArrayList<String>();
        combinationOne.add("11");
        combinationOne.add("12");
        final ArrayList<String> combinationTwo = new ArrayList<String>();
        combinationTwo.add("21");
        combinationTwo.add("22");
        final ArrayList<String> combinationThree = new ArrayList<String>();
        combinationThree.add("31");
        combinationThree.add("32");
        final ArrayList<String> combinationFour = new ArrayList<String>();
        combinationFour.add("41");
        combinationFour.add("42");

        JList list = new JList(items.toArray());

        MouseListener mouseListener = new MouseAdapter() {
            public void mouseReleased(MouseEvent mouseEvent) {
                JList eventList = (JList) mouseEvent.getSource();
                int index = eventList.locationToIndex(mouseEvent.getPoint());
                if (index >= -1) {
                    Object o = eventList.getModel().getElementAt(index);
                    System.out.println(o.toString());
                    if (combinationOne.contains(eventList.getSelectedValue())) {
                        eventList.addSelectionInterval(0, 0);
                        eventList.addSelectionInterval(4, 4);
                    } else if (combinationTwo.contains(eventList.getSelectedValue())) {
                        eventList.addSelectionInterval(1, 1);
                        eventList.addSelectionInterval(5, 5);
                    } else if(combinationThree.contains(eventList.getSelectedValue())) {
                        eventList.addSelectionInterval(2, 2);
                        eventList.addSelectionInterval(6, 6);
                    } else if (combinationFour.contains(eventList.getSelectedValue())) {
                        eventList.addSelectionInterval(3, 3);
                        eventList.addSelectionInterval(7, 7);
                    }
                }
            }
        };
        list.addMouseListener(mouseListener);

        list.setSelectionMode(
            ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        // Set the default selection
        list.addSelectionInterval(0, 0);
        list.addSelectionInterval(4, 4);

        JOptionPane.showMessageDialog(null, new JScrollPane(list));
    }
}

以下是此实现的一些问题:

  1. 单击鼠标时开始选择,但释放鼠标后发生第二次选择。这会造成不一致的印象。

  2. 您还会看到第二个项目选择有短暂的延迟,因为它是在鼠标释放后发生的。

  3. 第二个选定项目周围设置了边框,这是不需要的。

  4. 您可以使用向上/向下箭头在项目之间移动,但未处理。

而且,我不喜欢这种实现方式,因为它不是一种干净的方式。

我尝试过使用 ListSelectionListener,但我无法避免选择事件循环,导致它溢出。

最佳答案

看这个没有第四点。

    class Selections{

    ArrayList<String> items = new ArrayList<String>();
    final ArrayList<String> combinationOne = new ArrayList<String>();
    final ArrayList<String> combinationTwo = new ArrayList<String>();
    final ArrayList<String> combinationThree = new ArrayList<String>();
    final ArrayList<String> combinationFour = new ArrayList<String>();

    JList list = null;
    public Selections(){
        items.add("11");
        items.add("21");
        items.add("31");
        items.add("41");
        items.add("12");
        items.add("22");
        items.add("32");
        items.add("42");

        list = new JList(items.toArray());

        combinationOne.add("11");
        combinationOne.add("12");
        combinationTwo.add("21");
        combinationTwo.add("22");
        combinationThree.add("31");
        combinationThree.add("32");
        combinationFour.add("41");
        combinationFour.add("42");
    }
    public static void main(String[] args) throws Exception {
        Selectionsselections = new Selections();
        selections.runApp();

    }

    private void runApp() {
    MouseMotionListener mouseMotionListener = new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            JList eventList = (JList) e.getSource();
            addSelectionInterval(eventList);
        }
    };
        MouseListener mouseListener = new MouseAdapter() {
            public void mousePressed(MouseEvent mouseEvent) {
                JList eventList = (JList) mouseEvent.getSource();
                int index = eventList.locationToIndex(mouseEvent.getPoint());
                if (index >= -1) {
                    addSelectionInterval(eventList);
                }
            }
        };
        list.addMouseListener(mouseListener);
        list.addMouseMotionListener(mouseMotionListener );
        list.setCellRenderer(getRenderer());
        list.setSelectionMode(
                ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        // Set the default selection
        list.addSelectionInterval(0, 0);
        list.addSelectionInterval(4, 4);

        JOptionPane.showMessageDialog(null, new JScrollPane(list));
    }

    private void addSelectionInterval(JList eventList) {
        if (combinationOne.contains(eventList.getSelectedValue())) {
            eventList.addSelectionInterval(0, 0);
            eventList.addSelectionInterval(4, 4);
        } else if (combinationTwo.contains(eventList.getSelectedValue())) {
            eventList.addSelectionInterval(1, 1);
            eventList.addSelectionInterval(5, 5);
        } else if(combinationThree.contains(eventList.getSelectedValue())) {
            eventList.addSelectionInterval(2, 2);
            eventList.addSelectionInterval(6, 6);
        } else if (combinationFour.contains(eventList.getSelectedValue())) {
            eventList.addSelectionInterval(3, 3);
            eventList.addSelectionInterval(7, 7);
        }
    }

    private ListCellRenderer<? super String> getRenderer() {
        return new DefaultListCellRenderer(){
            @Override
            public Component getListCellRendererComponent(JList<?> list,
                                                          Object value, int index, boolean isSelected,
                                                          boolean cellHasFocus) {
                JLabel listCellRendererComponent = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected,cellHasFocus);
                listCellRendererComponent.setBorder(BorderFactory.createEmptyBorder());
                return listCellRendererComponent;
            }
        };
    }
}

关于java - JList - 设置允许的选择组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31452374/

相关文章:

java - 使用 BPEL 调用简单的 WSDL 服务

java - 使用 MenuBar 创建框架 - pack() 无法识别大小,菜单栏被截断

java - 从 JComboBox 弹出窗口中获取当前突出显示的项目(未选中的项目)

sql - 如何将事物的组合映射到关系数据库?

algorithm - 来自非统一表的所有可能组合,每列只有一个值

algorithm - 从 n 个元素的数组中查找 3 个元素的所有组合

java - 在构造函数上有值,但是当我想在另一个函数上使用它时,它为零(Android)

java - 测试两个持久性上下文是否相等

python - 如何在 TKinter 列表框中移动选择多个项目?

c# - 从 excel 粘贴到文本框