简体   繁体   English

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

[英]Using JComboBox as Cell Editor in JTable and saving changes

I am using a JComboBox as a cell editor for my JTable . 我正在使用JComboBox作为JTable的单元格编辑器。 When I select one of the values from the drop down list of the ComboBox, setValueAt is not getting called. 当我从ComboBox的下拉列表中选择值之一时,不会调用setValueAt I know this because I have overridden the function. 我知道这一点是因为我已经覆盖了该功能。 Based on the value selected in this cell, the value in another cell of the same table is fixed. 基于在此单元格中选择的值,同一表的另一个单元格中的值是固定的。 Also, I need to know which is the actionListener for this event, ie when I change the value in the ComboBox. 另外,我需要知道哪个是该事件的actionListener ,即何时更改ComboBox中的值。

The setValueAt does get called only when the focus is changed to another cell in the table, just clicking outside the table also does not help. 仅在将焦点更改为表中的另一个单元格时才调用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);
}

I just found the way... 我才找到路...

I need to add an actionListener to the JComboBox component that I created as a member of the CellEditor class and in the listener function, i need to call stopCellEditing so that the setValueAt gets called... 我需要将actionListener添加到作为CellEditor类的成员创建的JComboBox组件中,并在侦听器函数中,我需要调用stopCellEditing以便调用setValueAt ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM