当前位置: 代码迷 >> java >> JList-设置允许的选择组合
  详细解决方案

JList-设置允许的选择组合

热度:87   发布时间:2023-07-26 14:35:59.0

我有一个包含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;
            }
        };
    }
}