简体   繁体   English

JTable多项选择,不使用键盘,仅使用鼠标

[英]JTable multiple selection without using keyboard only using mouse

Here is my code. 这是我的代码。 I want to select multiple rows in JTable , I am using the following line: 我想在JTable选择多行,我在使用以下行:

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

With the above line I am able to select multiple rows using keyboard, but requirement is to select only using mouse. 在上面的行中,我可以使用键盘选择多个行,但要求仅使用鼠标进行选择。

Is there any thing other than this, that Java provides for multiple selection only using mouse without using keyboard? 除此之外,Java仅使用鼠标而不使用键盘提供了多种选择吗?

If you have this code, you need only yo push ctrl + click multiple. 如果您有此代码,则只需要yo push ctrl +单击多个即可。

Edit: But if you don't want to use keyboard it's possible i think, try this: 编辑:但是,如果您不想使用键盘,则可能是这样,请尝试以下操作:

Select Multiple Items In JList Without Using The Ctrl/Command Key 在不使用Ctrl / Command键的情况下在JList中选择多个项目

I don't think it's possible. 我认为这是不可能的。 I suggest adding an additional column to the table, containing a checkbox allowing to mark the row as selected. 我建议在表中增加一列,其中包含一个复选框,可将行标记为已选中。 Of course, you won't be able to use the table selection model to know which rows are selected. 当然,您将无法使用表选择模型来知道选择了哪些行。

Yes, you can select multiple rows without using keyboard by overriding the changeSelection function, like this: 是的,您可以通过覆盖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);
            }
        }

I finally used this code: 我终于使用了这段代码:

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

This way CTRL(toogle) is always pushed(true). 这样CTRL(toogle)总是被推入(true)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM