gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:10:00 26 4
gpt4 key购买 nike

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

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

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

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。

最佳答案

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

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

重置模型中的数据。即>

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

关于Java:JTable 内的 JComboBox - setSelectedIndex 对 GUI 没有影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15643082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com