【问题标题】:JTable multiple selection without using keyboard only using mouseJTable多选不使用键盘只使用鼠标
【发布时间】:2017-06-08 16:48:15
【问题描述】:

这是我的代码。我想在JTable 中选择多行,我使用的是以下行:

table.getColumnModel().getSelectionModel().setSelectionMode(
    javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

通过上述行,我可以使用键盘选择多行,但要求只能使用鼠标进行选择。

除此之外,Java 是否提供了仅使用鼠标而不使用键盘进行多选的功能?

【问题讨论】:

  • 我错过了provides for multiple selection only using mouse without using keyboard 不可能直接,因为 ListSelectionModel 只是一维的,这在 API 中仅针对 JList 和 JTree 实现

标签: java swing jtable


【解决方案1】:

我认为这是不可能的。我建议在表格中添加一个附加列,其中包含一个复选框,允许将该行标记为选中。当然,您将无法使用表格选择模型来知道选择了哪些行。

【讨论】:

  • +1,如果他将复选框值绑定到他的表格模型中的选择模型,他可以使用选择模型。
【解决方案2】:

如果你有这个代码,你只需要你 push ctrl + click multiple。

编辑:但如果你不想使用键盘,我认为可以试试这个:

Select Multiple Items In JList Without Using The Ctrl/Command Key

【讨论】:

  • 如何只用鼠标按 ctrl?
【解决方案3】:

是的,您可以通过覆盖 changeSelection 函数而无需使用键盘来选择多行,如下所示:

@Override
        public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
            latestClickedRowIndex = rowIndex;
            ListSelectionModel selectionModel = getSelectionModel();
            boolean selected = selectionModel.isSelectedIndex(rowIndex);
            //throw new UnsupportedOperationException("Paila.");
            if (selected) {
                selectionModel.removeSelectionInterval(rowIndex, rowIndex);
                getValueAt(rowIndex, columnIndex);
            } else {
                selectionModel.addSelectionInterval(rowIndex, rowIndex);
            }
        }

【讨论】:

    【解决方案4】:

    我终于用上了这段代码:

    JTable table = new JTable(){
        @Override
        public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
            super.changeSelection(rowIndex, columnIndex, true, extend);
        }
    };
    

    这样 CTRL(toogle) 总是被推送(true)。

    【讨论】:

      猜你喜欢
      • 2017-08-06
      • 1970-01-01
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 2021-04-10
      相关资源
      最近更新 更多