gpt4 book ai didi

java - JTable 单元格重叠

转载 作者:行者123 更新时间:2023-12-01 21:43:31 25 4
gpt4 key购买 nike

我已经实现了 JTree 并填充了一些数据。该表包含三列,并且根据特定单元格上的某些值,它应该具有标签或组合框。第三列中的所有值都是可编辑的。我也有一个 JTree,从中选择一个节点,并基于它,表值相应地改变。当我编辑表中的单元格并移动到树中的另一个节点(在表中填充一组新数据)时,存在问题,之前编辑的单元格值存在于表中新单元格值的顶部。下面是我如何实现 TableCellRenderer 和 TableCellEditor。由于我是挥杆初学者,我可能使用了一些错误的概念。请帮我找出我做错了什么。

        public void populateTableData(List<Field> list,JTree jTree){
fieldList = null;
tcBuilderTree = jTree;
fieldList = list;
md=new PropertiesTableModel(fieldList);
getPropertieseTable().setModel(md);

final TableCellRenderer cellRenderer = new TableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable arg0,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int col) {

if(value instanceof List<?>) {
List<Value> valueList=(ArrayList)value;
return createComboBox(valueList);
}
else{
JLabel lbl=new JLabel();
lbl.setText((String)value);
return lbl;
}

}
};
propertiesTable.setDefaultRenderer(Object.class, cellRenderer);

final TableCellEditor cellEditor = new TableCellEditor() {
private DefaultCellEditor textEditor;
private DefaultCellEditor currentEditor;
@Override
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column) {
textEditor = new DefaultCellEditor(new JTextField());
PropertiesTableModel model = (PropertiesTableModel) table.getModel();
List<Value> values = model.getPossibleValues(row, column);
if (values != null) {
List<Value> valueList=(ArrayList)value;
currentEditor = new DefaultCellEditor(createComboBox(valueList));
} else {
currentEditor = textEditor;
}
return currentEditor.getTableCellEditorComponent(table, value,
isSelected, row, column);
}

@Override
public Object getCellEditorValue() {
return currentEditor.getCellEditorValue();
}

@Override
public boolean isCellEditable(EventObject anEvent) {
JTable tbl = (JTable) anEvent.getSource();
int row, col;
if (anEvent instanceof MouseEvent) {
MouseEvent evt = (MouseEvent) anEvent;
row = tbl.rowAtPoint(evt.getPoint());
col = tbl.columnAtPoint(evt.getPoint());
} else {
row = tbl.getSelectedRow();
col = tbl.getSelectedColumn();
}
if(col<2){
return false;
}
else
{
return true;
}
}

@Override
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}

@Override
public boolean stopCellEditing() {
Object obj = currentEditor.getCellEditorValue();
fieldList.get(propertiesTable.getEditingRow())
.setDefaultValue(obj);
return currentEditor.stopCellEditing();
}

@Override
public void cancelCellEditing() {
currentEditor.cancelCellEditing();
}

@Override
public void addCellEditorListener(CellEditorListener l) {
}

@Override
public void removeCellEditorListener(CellEditorListener l) {

}
};
propertiesTable.setDefaultEditor(Object.class,cellEditor);
}

enter image description here

最佳答案

The issue exists when I edit a cell in the table and move to another node in the tree (which populates a new set of data in the table), the previously edited cell values exist in the table on top of the new cell values.

我猜您在使用新数据重新加载表格之前不会停止对表格单元格的编辑。

创建 JTable 时,您可能应该将以下内容添加到代码中:

JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

参见Table Stop Editing了解更多信息。

The table contains three columns and based on some values on a specific cell it should have either labels or combo boxes

您可能不需要创建自定义编辑器。只需让表格根据正在编辑的单元格选择适当的默认编辑器即可。您可以通过重写 JTable 的 getCellEditor(...) 方法来完成此操作。查看:How to add unique JComboBoxes to a column in a JTable (Java)有关此方法的示例。

关于java - JTable 单元格重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36192830/

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