2011-01-23 175 views
1

我创建一个dataTable和cellEditor一列。这个列简单jSpinner。我有以下问题。当我在微调器中输入一些值并选择另一行时,上一行中的值将不会更改。如果我按下,它会完成。如果我选择或按钮,它也会完成。但是如果我输入价值并改变选择,它将不会完成。请帮助。这是CellEditor代码。JSpinner更新

public class DurationTableCellEditor extends AbstractCellEditor implements TableCellEditor{ 

final JSpinner spinner = new JSpinner(); 

// Initializes the spinner. 
public DurationTableCellEditor() { 
    spinner.setModel(new SpinnerNumberModel(1,1,50000,1));   
} 

// Prepares the spinner component and returns it. 
public Component getTableCellEditorComponent(JTable table, Object value, 
     boolean isSelected, int row, int column) { 
    spinner.setValue(new Integer(value.toString()).intValue()); 
    spinner.setCursor(null); 
    return spinner; 
} 

// Enables the editor only for double-clicks. 
@Override 
public boolean isCellEditable(EventObject evt) { 
    if (evt instanceof MouseEvent) { 
     return ((MouseEvent)evt).getClickCount() >= 1; 
    } 
    return true; 
} 

// Returns the spinners current value. 
public Object getCellEditorValue() { 
    return spinner.getValue(); 
} 

}

回答

0

目前尚不清楚你如何更新你的数据模型,而是一个办法是实施ChangeListenerCellEditor,竟有这样example实现ItemListener。作为参考,请参阅How to Use Tables: Using Other Editors。特别是,看看fireEditingStopped()。最后,您需要相应的TableCellRenderer

0

commitEdit()

// Returns the spinners current value. 
public Object getCellEditorValue() { 
    spinner.commitEdit(); 
    return spinner.getValue(); 
}