[英]Detecting if any cell is being edited in a JTable
I have a JTable in which users can edit cells. 我有一个JTable,用户可以在其中编辑单元格。 When the user is through editing the cell, I want to enable a button that allows the user to validate the input.
当用户通过编辑单元格时,我想启用一个按钮,该按钮允许用户验证输入。 However, I only want to do this when the user is no longer in edit mode.
但是,我只想在用户不再处于编辑模式时执行此操作。 Is there an easy way for me to detect if any cell in a Jtable is currently being edited?
我有一种简单的方法可以检测Jtable中是否正在编辑任何单元格吗?
Thank you, 谢谢,
Even easier: 更简单:
if (!table.isEditing())
//
It turns out that the Jtable has a way to detect if any cell is being edited. 事实证明,Jtable有一种方法可以检测是否正在编辑任何单元格。 It is actually very simple.
实际上非常简单。 You simply check if myJtable.getCellEditor() == null.
您只需检查myJtable.getCellEditor()== null。 If it is not, you are editing a cell, if it is, no cells are being edited.
如果不是,则您正在编辑一个单元格;如果是,则没有正在编辑任何单元格。
class TableEditor extends DefaultCellEditor{
JTextField com;
public TableEditor(){
getComponent().addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e){
com.setBackground(new Color.red);
}
});
}
}
And then 接着
tb.setDefaultEditor(Object.class,new TableEditor());
I would use a custom TabelModel
and override the setValueAt
function to perform your validation. 我将使用自定义的
TabelModel
并覆盖setValueAt
函数来执行验证。 It should automatically be called whenever a user is done editing a table cell. 每当用户完成编辑表格单元格时,都应自动调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.