【问题标题】:Select Multiple Items In JList Without Using The Ctrl/Command Key在不使用 Ctrl/Command 键的情况下选择 JList 中的多个项目
【发布时间】:2011-01-25 03:53:12
【问题描述】:

我正在寻找一种方法,只需单击每个项目即可在 JList 中选择多个项目。

执行此操作的正常方法是按住 command/ctrl 键,然后单击。

我认为只允许用户单击打开和关闭项目而不需要按住额外的键会更直观。

【问题讨论】:

  • 我的应用程序是针对非计算机知识的,所以我觉得我不需要使用标准(著名的遗言)。我从经验中发现,人们有时会忘记按住 ctrl 并取消选择所有其他项目。我打算做一些可用性测试,看看人们喜欢什么。
  • 一个时髦的非标准应用程序(如 Lotus Notes)的配方..
  • 绝对有道理。我正在测试,没有文档/教程说您必须按 Control 进行多项选择...正在测试和测试,发现代码没有问题...+1。

标签: java swing user-interface


【解决方案1】:

在更改默认行为之前三思而后行。除非您有一些特殊的用例,否则我不希望我的 List 与其他任何地方都不同:)

话虽如此,你应该可以使用自己的ListSelectionModel:

list.setSelectionModel(new DefaultListSelectionModel() {
    @Override
    public void setSelectionInterval(int index0, int index1) {
        if(super.isSelectedIndex(index0)) {
            super.removeSelectionInterval(index0, index1);
        }
        else {
            super.addSelectionInterval(index0, index1);
        }
    }
});

【讨论】:

  • 感谢您的款待。我不相信这是正确的做法,但我想和几个人一起测试一下。
  • 它正在工作,但请注意该列表上的任何 ListSelectionListener 都不会收到通知。要解决这个问题,请在方法末尾添加 fireValueChanged(index0, index1);。
  • 正常工作,但如果在按下按钮时将鼠标移动一个像素,则不会。然后它再次被取消选择:-(
【解决方案2】:
list.setSelectionModel(new DefaultListSelectionModel() {
    private int i0 = -1;
    private int i1 = -1;

    public void setSelectionInterval(int index0, int index1) {
        if(i0 == index0 && i1 == index1){
            if(getValueIsAdjusting()){
                 setValueIsAdjusting(false);
                 setSelection(index0, index1);
            }
        }else{
            i0 = index0;
            i1 = index1;
            setValueIsAdjusting(false);
            setSelection(index0, index1);
        }
    }
    private void setSelection(int index0, int index1){
        if(super.isSelectedIndex(index0)) {
            super.removeSelectionInterval(index0, index1);
        }else {
            super.addSelectionInterval(index0, index1);
        }
    }
});

【讨论】:

    【解决方案3】:

    我认为您可以通过在 JList 上附加鼠标侦听器并以编程方式在侦听器代码中选择项目来轻松完成此操作。当然,您可能需要一些代码来根据某些坐标确定按下了哪个项目。

    【讨论】:

      猜你喜欢
      • 2011-01-23
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      相关资源
      最近更新 更多