Java JTable - 如何强制用户只选择一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18309113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:21:53  来源:igfitidea点击:

JTable - How to force user to select exactly one row

javaswingjtable

提问by guitar_freak

I have to implement a JTable in which exactly one row has to be selected (always). Empty selection is not allowed. I'm selecting the first row during initialization:

我必须实现一个 JTable,其中必须选择(总是)一行。不允许空选。我在初始化期间选择第一行:

table.setRowSelectionInterval(0, 0);

additionally, I'm using

另外,我正在使用

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

But user can still deselect one row using CLick + Ctrl.

但是用户仍然可以使用 CLick + Ctrl 取消选择一行。

What is the easiest way ensure, that one (exaclty) row is always selected in the table ?

确保始终在表中选择一行(exaclty)的最简单方法是什么?

采纳答案by MadProgrammer

Now, you could add MouseListeners, SelectionListeners, KeyListeners and key bindings to try and solve this is issue. Or, you could go to the heart of the problem.

现在,您可以添加MouseListeners、SelectionListeners、KeyListeners 和键绑定来尝试解决这个问题。或者,您可以直击问题的核心。

The ListSelectionModelis responsible for managing the selection details.

ListSelectionModel负责管理该选择的详细信息。

You could simply supply your own ListSelectionModelfor the row selection

您可以简单地ListSelectionModel为行选择提供自己的

public class ForcedListSelectionModel extends DefaultListSelectionModel {

    public ForcedListSelectionModel () {
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    @Override
    public void clearSelection() {
    }

    @Override
    public void removeSelectionInterval(int index0, int index1) {
    }

}

And simply set it to your table...

只需将它放在你的桌子上......

table.setSelectionModel(new ForcedListSelectionModel());

回答by mKorbel

What is the easiest way ensure, that one (exaclty) row is always selected in the table ?

确保始终在表中选择一行(exaclty)的最简单方法是什么?

there are three (basically) selection types

有三种(基本上)选择类型

JTable.setRowSelectionAllowed(boolean);
JTable.setColumnSelectionAllowed(boolean);
JTable.setCellSelectionAllowed(boolean);

edit

编辑

works for me too

对我也有用

int row = table.getSelectedRow();
    if ((row > -1)) {
        table.setRowSelectionInterval(row, row);
    }
}

回答by blackpanther

I would use the JTable#setRowSelectionAllowedas it will ensure that a row can be selected.

我会使用JTable#setRowSelectionAllowed因为它会确保可以选择一行。

回答by Daniel Villanueva

If you have a JTable instance created, just use:

如果您创建了 JTable 实例,只需使用:

jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

回答by Matt Reinhart

This code changes it right back to the desired index and it gives the appearance to the user that it never updates.

此代码将其立即更改回所需的索引,并为用户提供永远不会更新的外观。

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(
    new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            table.setRowSelectionInterval(0,0);
        }
);

回答by indika

First do

先做

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setRowSelectionInterval(0, 0);

then set ListSelectionModel

然后设置 ListSelectionModel