2011-01-23 181 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(); 
}