select 来自特定列的 jtable 单元格

select jtable cells from particular column

我有一个 JTable,我只需要允许从 column2 中进行选择。如果用户选择 row1 x column3,则选择应更改为 row1 x column2.

例如。在下面的 table 中:-

    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)

实现此目的最简单的方法是什么?

一个选项是处理select离子变化事件和select特定列:

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);