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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:36:10  来源:igfitidea点击:

How to get value of a JTable cell during edit

javaswingjtablelistener

提问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-valueand new-valueof the edited cell in the table.

使用TableCellListener来监听TableModel. 通过使用这个你可以得到row-indexcolumn-indexold-valuenew-value在表中编辑的单元格的。