java 如何在编辑期间获取 JTable 单元格的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14539827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get value of a JTable cell during edit
提问by Mark09
The solution I have seen so far is listen cell change i.e.
到目前为止,我看到的解决方案是监听单元更改,即
TableModelListener tableModelListener = new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.UPDATE) {
row = e.getFirstRow();
col = e.getColumn();
// do something
}
}
};
But I need to get the cell value, selectedRow & selectedColumn when typing, and before hitting enter. How to do it?
但是我需要在键入时以及在按 Enter 之前获取单元格值 selectedRow 和 selectedColumn。怎么做?
采纳答案by trashgod
Use a DocumentListener
, illustrated here, or a DocumentFilter
, seen here, in your TableCellEditor
, shown here.
使用 a DocumentListener
,此处说明,或 a DocumentFilter
,此处看到,在您的TableCellEditor
,此处显示。
回答by c.pramod
HINT :
暗示 :
if (jTable1.getCellEditor() == null) {
System.out.println("Not Edited");
} else {
System.out.println(jTable1.getValueAt(jTable1.getSelectedRow(),jTable1.getSelectedColumn()));
}
where jTable1 is your JTable Name
其中 jTable1 是您的 JTable 名称
回答by Amarnath
Use TableCellListenerfor listening to the changes in the TableModel
. By using this you can get the row-index
, column-index
, old-value
and new-value
of the edited cell in the table.
使用TableCellListener来监听TableModel
. 通过使用这个你可以得到row-index
,column-index
,old-value
并new-value
在表中编辑的单元格的。