我有一个JTable,我需要只允许从 column2 中进行选择。如果用户选择 row1 x column3 ,则选择应更改为 row1 x column2 。
例如。在下表中: -
co1 co2 co3 co4
ro1 a b c d
ro2 e f g h
ro3 i j k l
ro4 m n o p
当用户选择单元格" a"," b"," c"或者" d" ....选择应该改为单元格" b" (的 列2 )
与e,f,g,h - >相同; f( column2 )
与i,j,k,l - >相同; j( column2 )
与m,n,o,p - >相同; n( column2 )
实现这一目标的最简单方法是什么?
答案 0 :(得分:1)
一个选项是处理选择更改事件并选择特定列:
final int columnIndex = 2; //index of your column
//listener that changes selection.
ListSelectionListener selectParticularColumnListener = new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
//this method should have access to your JTable
table.setColumnSelectionInterval(columnIndex, columnIndex);
}
};
//listen for row selection changes
table.getSelectionModel().addListSelectionListener(selectParticularColumnListener);
//listen for column selection changes
table.getColumnModel().getSelectionModel().addListSelectionListener(selectParticularColumnListener);