gpt4 book ai didi

java - 在别处单击时,JTable ComboBox 会丢失值

转载 作者:搜寻专家 更新时间:2023-11-01 03:51:03 25 4
gpt4 key购买 nike

我已经为 JTable 单元格设置了一个 jCombobox 作为 DefaultCellEditor

当我在单元格 (jCombobox) 中键入一个值时,问题就来了,每当我单击其他地方时,该值就会丢失。有人知道为什么以及我该如何解决这个问题?

table.getColumnModel().getColumn(1).setCellEditor(new SpringJobTablePopupCellEditor());

public class SpringJobTablePopupCellEditor extends AbstractCellEditor implements TableCellEditor {

JTextField jtf;

DefaultCellEditor other;
DefaultCellEditor checkbox;
private DefaultCellEditor lastSelected;
JComboBox cbox = null;
public SpringJobTablePopupCellEditor() {
jtf = new JTextField();
jtf.setDocument(new JTextFieldLimit(1000));

other = new DefaultCellEditor(jtf);
checkbox = new DefaultCellEditor(generateBox("10"));
}

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

@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
final JTable t = table;
cbox.getEditor().getEditorComponent().addFocusListener(new FocusListener() {

@Override
public void focusGained(FocusEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void focusLost(FocusEvent e) {
if(t.isEditing()){
t.getCellEditor().stopCellEditing();
}
}
});
String val = table.getModel().getValueAt(row, column - 1).toString();

if("ak".equals(val)){
lastSelected = checkbox;
return checkbox.getTableCellEditorComponent(table, value, isSelected, row, column);
}
lastSelected = other;
return other.getTableCellEditorComponent(table, value, isSelected, row, column);
}

private JComboBox generateBox(String type) {

cbox = new JComboBox();
cbox.setEditable(true);
for (Map.Entry<String, String> entry : SpringJob.akMap.entrySet()) {
cbox.addItem(entry.getValue());
}
return cbox;
}

}

最佳答案

尝试

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

当您从一个组合框转到另一个组合框时,您需要强制表格停止编辑我通过向组合框添加 focusListener 并在 focuslost 添加中实现了类似的效果

public void focusLost(FocusEvent e) {
if (table.isEditing())
table.getCellEditor().stopCellEditing();
}
}

关于java - 在别处单击时,JTable ComboBox 会丢失值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27960771/

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