Jtable中的JComboBox不会在鼠标单击时停止编辑

时间:2015-10-18 16:45:14

标签: java swing jtable jcombobox tablecelleditor

我有一个包含10行和2列的Jtable

列nb 2是可编辑的JCombobox

当我在JCombobox中手动输入一些值时,当我按Enter键时,该值仍然存在。

但是当我在JCombobox中放入一些值并用鼠标点击某个其他单元格时,该值被分配给该单元格而不是我正在编辑的单元格。

基本上,当焦点从该单元格中丢失时,它不会停止编辑单元格。

当我点击下一个单元格并为该单元格分配新值时,它会停止编辑。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您应该在JCombobox组件上添加焦点侦听器,如果丢失焦点,则应在表格单元格编辑器中停止编辑。例如:

public class TestTableCellEditor extends DefaultCellEditor 
                                      implements FocusListener {
private JComboBox comboBox;

public TestTableCellEditor(JComboBox comboBox) {
   this.comboBox = comboBox;
   comboBox.addFocusListener(this);       
}
// ... Some other things in the editor

public void focusGained(FocusEvent e) {
// You don't need to do anything here
}
public void focusLost(FocusEvent e) {        
    stopCellEditing();        
}
}
相关问题