【问题标题】:make only one checkbox selectable in JTable Column在 JTable 列中只选择一个复选框
【发布时间】:2015-07-01 07:32:39
【问题描述】:

我有一个有 2 个列的 JTable,一列是对象类型,第二列是复选框。在该表中,用户只想选择一个复选框。我尝试了很多代码(例如:jtable checkbox single selectionSecond one),最后我使用函数完成了这个。

这是我的功能:

    public void setSelectionEnableOrDisable(JTable table) {
    int earlierSelectionRow = 0;
    int selectedRow = table.getSelectedRow();
    for (int i = 0; i < table.getRowCount(); i++) {
        if ((Boolean) table.getValueAt(i, 1) == true && i != selectedRow) {
            earlierSelectionRow = i;
        }
    }
    table.setValueAt(true, selectedRow, 1);
    table.setValueAt(true, earlierSelectionRow, 1);
}

但是,这个问题是什么,当我慢慢点击复选框时,它很好。如果我快速单击 2 或 3 个复选框,那么我的代码允许多选。这里有什么问题?

【问题讨论】:

标签: java jtable


【解决方案1】:

我认为你可以做得更简单,比如:

 public void setSelectionEnableOrDisable(JTable table) {

 int selectedRow = table.getSelectedRow();
 if ((Boolean)table.getValueAt(selectedRow , 1)) {
    for (int i = 0; i < table.getRowCount(); i++) {
    if ( i != selectedRow) {
       table.setValueAt(false, i, 1);
    }
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多