我有一個JTable和內上我的JComboBox列。的JComboBox恢復到原來的值在JTable中
我的表都有自己的模式:見下文
package View;
public class CustomTableModel extends AbstractTableModel {
ArrayList<Item> data;
public String[] columnNames = {"ID", "Amount", "value", "bought", "quantity"};
public CustomTableModel(ArrayList<Item> data){
this.data = data
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.size();
}
@Override
public Object getValueAt(int arg0, int arg1) {
if(arg1 == 0){
return data.get(arg0).getID();
}
if(arg1 == 1){
return data.get(arg0).getAmount();
}
if(arg1 == 2){
return data.get(arg0).getValue();
}
if(arg1 == 3){
return data.get(arg0).isBought();
}
else {
return data.get(arg0).getQuantity();
}
}
public String getColumnName(int index) {
return columnNames[index];
}
@Override
public void setValueAt(Object aValue, int row, int col) {
if (col == 3) {
data.get(row).setBought((Boolean) aValue);
this.fireTableCellUpdated(row, col);
}
}
public boolean isCellEditable(int row, int col){
return true;
}
public Class<?> getColumnClass(int columnIndex)
{
if (columnIndex == 3)
{
return Boolean.class;
}
else
return super.getColumnClass(4);
}
而這種渲染和編輯應用到持有JComboBox時列;
public class ComboBoxRenderer extends JComboBox implements TableCellRenderer {
public ComboBoxRenderer(String[] items) {
super(items);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelectedIndex(0);
return this;
}
public class ComboBoxEditor extends DefaultCellEditor {
public ComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
public Object getCellEditorValue() {
return j.getEditor().getItem();
}
的對話框出現,因爲它應該與它要把所有的值,但是當我點擊框並選擇另一個值會當我瀏覽頁面上的其他地方恢復到原來的值。然而有趣的是,如果我點擊列中的JCombobox的任何一個,它將突出顯示我選擇的新值!但它只會顯示原始信息。
爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-02-24 11:09:15