Java:JTable中的JComboBox - setSelectedIndex对GUI没有影响

时间:2013-03-26 17:01:08

标签: java swing indexing jtable jcombobox

我有一个有两列的JTable。在第二列中,每行有一个不同的编辑器(JTextField,JComboBox和CheckComboBox)。到目前为止,这工作正常但是我已经实现了一个重置​​选项,它将整个JTable更改回原始状态(重置所有更改)。

我现在面临的问题是,当我使用setSelectedIndex编程更改ComboBox的索引时,我看到GUI中没有结果,尽管模型使用fireTableDataChanged触发其更改并且也被表的TableModelListener。当我查找更改的ComboBox时,我也得到了正确的索引,但它没有显示在GUI中。我还尝试了revalidateupdateUIrepaint方法,但没有任何变化。

问题可能在于它的架构(也许是渲染器?)。这是我的编辑/渲染类。

class VEachRowEditor implements TableCellEditor, TableCellRenderer {

protected Hashtable<Integer, TableCellEditor> editors;
protected TableCellEditor editor, defaultEditor, renderer;
JTable table;
VEachRowEditorManager rowmanager;

public VEachRowEditor(JTable table, VEachRowEditorManager rowmanager) {
    this.table = table;
    editors = new Hashtable<Integer, TableCellEditor>();
    defaultEditor = new DefaultCellEditor(new JTextField());
    this.rowmanager = rowmanager;
}

public void setEditorAt(int row, TableCellEditor editor) {
    if (editor instanceof DefaultCellEditor)
        ((DefaultCellEditor) editor).setClickCountToStart(1);
    editors.put(new Integer(row), editor);
}

public Component getTableCellEditorComponent(JTable table, Object value,
        boolean isSelected, int row, int column) {
    editor = (TableCellEditor) editors.get(new Integer(row));
    if (editor == null) {
        editor = defaultEditor;
    }
    return editor.getTableCellEditorComponent(table, value, isSelected,
            row, column);
}

public Object getCellEditorValue() {
    return editor.getCellEditorValue();
}

public final boolean stopCellEditing() {
    return editor.stopCellEditing();
}

public void cancelCellEditing() {
    editor.cancelCellEditing();
}

public boolean isCellEditable(EventObject anEvent) {
    selectEditor((MouseEvent) anEvent);
    return editor.isCellEditable(anEvent);
}

public void addCellEditorListener(CellEditorListener l) {
    editor.addCellEditorListener(l);
}

public void removeCellEditorListener(CellEditorListener l) {
    editor.removeCellEditorListener(l);
}

public boolean shouldSelectCell(EventObject anEvent) {
    selectEditor((MouseEvent) anEvent);
    return editor.shouldSelectCell(anEvent);
}

protected void selectEditor(MouseEvent e) {
    int row;
    if (e == null) {
        row = table.getSelectionModel().getAnchorSelectionIndex();
    } else {
        row = table.rowAtPoint(e.getPoint());
    }
    editor = (TableCellEditor) editors.get(new Integer(row));
    if (editor == null) {
        System.out.println(editor);
        editor = defaultEditor;
    }
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    renderer = (TableCellEditor) editors.get(new Integer(row));
    if (renderer == null) {
        renderer = defaultEditor;
    }

    return renderer.getTableCellEditorComponent(table, value, isSelected,
            row, column);
}

}

getTableCellEditorComponent错了吗?

rowmanager包含所有模型的所有JComboBox和CheckComboBox。

1 个答案:

答案 0 :(得分:2)

  

当我使用setSelectedIndex以编程方式更改ComboBox的索引时,我看不到GUI中的结果

渲染器和编辑器只显示模型中的数据。不要重置编辑器组件。

重置模型中的数据。即&GT;

table.setValueAt(...); // or
table.getModel().setValueAt(...);