0

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

我现在面临的问题是,当我以编程方式更改组合框的索引时,setSelectedIndex我在 GUI 中看不到任何结果,尽管模型触发了它的更改,fireTableDataChanged并且也被TableModelListener表中的一个接收到。当我查找更改后的 ComboBox 时,我也得到了正确的索引,但它没有显示在 GUI 中。我也尝试了这些方法revalidate,但updateUI没有repaint任何改变。

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

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包含所有模型的所有 JComboBoxes 和 CheckComboBoxes 。

4

1 回答 1

2

当我使用 setSelectedIndex 以编程方式更改组合框的索引时,我在 GUI 中看不到任何结果

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

重置模型中的数据。即>

table.setValueAt(...); // or
table.getModel().setValueAt(...);
于 2013-03-26T17:24:35.470 回答