使用JComboBox作为JTable中的单元格编辑器并保存更改



我正在使用JComboBox作为JTable的单元格编辑器。当我从组合框的下拉列表中选择其中一个值时,不会调用setValueAt。我知道这一点是因为我已经覆盖了函数。根据在此单元格中选择的值,同一表的另一个单元格中的值是固定的。此外,我需要知道哪个是此事件的actionListener,即当我更改组合框中的值时。

只有当焦点更改为表中的另一个单元格时,才会调用setValueAt,仅在表外单击也没有帮助。

@Override 
public void setValueAt(Object o,int row,int col)
{
    super.setValueAt(o, row, col);
    if(((String)o).matches("1"))
    {
        super.setValueAt(o, col-1, row+1);
        return;
    }
    if(((String)o).contains("/"))
        super.setValueAt(((String)o).substring(2), col-1, row+1);
    else
        super.setValueAt("1/"+(String)o, col-1, row+1);
}

我刚刚找到了。。。

我需要向我作为CellEditor类成员创建的JComboBox组件添加一个actionListener,在listener函数中,我需要调用stopCellEditing,以便调用setValueAt。。。

最新更新