我有一个应用程序,当用户按下按钮时,会向JTable添加“图层”。应用程序在创建新文件时添加第一层。我希望在JTable中自动选择这一层。有谁知道如何做到这一点?
答案 0 :(得分:1)
您可以通过JTable的选择模型控制选择。
myJTable.getSelectionModel().setSelectionInterval( index, index );
答案 1 :(得分: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();