我已经定了一个计划 jCombobox
作为 DefaultCellEditor
为了一个 JTable
牢房。
当我在单元格中键入一个值时,问题就出现了( 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;
}
}
2条答案
按热度按时间nwlqm0z11#
尝试
当你从一个组合框转到另一个组合框时,你需要强制你的表停止编辑,我通过在组合框和focuslost中添加focuslistener实现了类似的效果
hxzsmxv22#
组合框通常存储了多个项目列表。在jtable中单击jcombo框并键入值时,它不会直接添加到combobox列表项中。必须先在组合框中添加值。组合框包含对象数组。
尝试在jtable组合框中动态添加值