0

我有一个应用程序,当用户按下按钮时,它会向 JTable 添加“层”。应用程序在创建新文件时添加第一层。我希望在 JTable 中自动选择该层。有谁知道如何做到这一点?

4

2 回答 2

1

您可以通过 JTable 的选择模型来控制选择。

 myJTable.getSelectionModel().setSelectionInterval( index, index );
于 2013-04-03T19:38:27.713 回答
0

简单情况:

//if needed set selection model
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//at first, select row
table.getSelectionModel().setSelectionInterval(from, to);
//then select column if needed
table.setColumnSelectionInterval(from, to)
//at the end request focus
table.requestFocusInWindow();

更困难的是,您有一个允许多行选择的表格:

table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
//select needed lines
ListSelectionModel model = table.getSelectionModel();
model.clearSelection();
int[] focusedLines = new int[] {0, 4, 10};
for(int actualRow : focusedLines)
{
    model.addSelectionInterval(actualRow, actualRow);
}
//then select column if needed
table.setColumnSelectionInterval(from, to)
//at the end request focus
table.requestFocusInWindow();
于 2013-04-03T19:48:14.237 回答