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

时间:2023-01-28 22:35:01

I am having a JTable with two columns. In the second column there are different editors (JTextField, JComboBox and CheckComboBox), in each row one. This works fine so far however I have implemented a reset option which changes the whole JTable back to the original state (resets all changes).

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

The problem I am facing now is that when I programmatical change the index of a ComboBox with setSelectedIndex I see no result in the GUI although the model fires its change with fireTableDataChanged and is also receivied by an TableModelListener of the Table. When I lookup the changed ComboBox I also get the correct index however it is not shown in the GUI. I also tried the methods revalidate, updateUI and repaint wihtout any change.

我现在面临的问题是,当我用setSelectedIndex对ComboBox的索引进行编程时,我在GUI中看不到结果,尽管模型使用fireTableDataChanged触发它的更改,并且也由表的TableModelListener接收。当我查找已更改的ComboBox时,我也得到了正确的索引,但是在GUI中没有显示。我还尝试了方法revalidate、updateUI和repaint以避免任何更改。

The problem might lay in the architecture of it (maybe the Renderer?). Here is my Editor/Renderer class.

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

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);
}

}

}

Is the getTableCellEditorComponent wrong?

getTableCellEditorComponent错了吗?

The rowmanager holds all the JComboBoxes and CheckComboBoxes with all the models.

rowmanager持有所有jcombobox和checkcombobox以及所有模型。

1 个解决方案

#1


2  

when I programmatical change the index of a ComboBox with setSelectedIndex I see no result in the GUI

当我用setSelectedIndex对组合框的索引进行编程时,我在GUI中看不到结果

Renderer and editers just display the data in the model. Don't reset the editor component.

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

Reset the data in the model. ie>

重置模型中的数据。ie >

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

#1


2  

when I programmatical change the index of a ComboBox with setSelectedIndex I see no result in the GUI

当我用setSelectedIndex对组合框的索引进行编程时,我在GUI中看不到结果

Renderer and editers just display the data in the model. Don't reset the editor component.

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

Reset the data in the model. ie>

重置模型中的数据。ie >

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