【问题标题】:How to get value of a JTable cell during edit如何在编辑期间获取 JTable 单元格的值
【发布时间】:2013-01-26 17:57:54
【问题描述】:

到目前为止我看到的解决方案是听单元格变化,即

    TableModelListener tableModelListener = new TableModelListener() {

        @Override
        public void tableChanged(TableModelEvent e) {
            if (e.getType() == TableModelEvent.UPDATE) {

                row = e.getFirstRow();
                col = e.getColumn();

                // do something
            }
        }
    };

但我需要在键入时以及在按 Enter 之前获取单元格值、selectedRow 和 selectedColumn。怎么做?

【问题讨论】:

  • 我需要在输入时获取单元格值、selectedRow 和 selectedColumn,然后再按回车 - 听起来很奇怪:编辑时它不会改变,所以你可以抓住它在开始编辑之前...

标签: java swing jtable listener


【解决方案1】:

在您的TableCellEditor 中使用DocumentListener,图示为here,或DocumentFilter,见here,显示为here

【讨论】:

  • 感谢您提供的信息,但我还是 Java 新手,需要更多时间来了解这些。
  • 你肯定需要CellEditor,它会告诉你row & column;请随时更新您的问题。
  • @trashgod,请根据您提供的链接提供在扩展DefaultCellEditor 上放置DocumentListener 的示例。我需要重写什么方法?我也有同样的问题。谢谢。
  • 将侦听器添加到CellEditor 使用的文本组件,在引用的示例中为JTextField。如果您提出新问题,请联系我。
【解决方案2】:

提示:

 if (jTable1.getCellEditor() == null) {
                    System.out.println("Not Edited");

                } else {

            System.out.println(jTable1.getValueAt(jTable1.getSelectedRow(),jTable1.getSelectedColumn()));
    }

其中 jTable1 是您的 JTable 名称

【讨论】:

  • 这个问题有点奇怪,但不管它真正想要什么——这不是分析器;-)
  • 这个答案没有错,不完全适合您的特定需求(因为它们无论如何都很难实现)但它帮助了我
【解决方案3】:

使用TableCellListener 监听TableModel 的变化。使用这个可以得到表格中编辑单元格的row-indexcolumn-indexold-valuenew-value

【讨论】:

  • 我的需求和 Mubashir 完全一样:这需要按回车键,我需要的是当值改变时,事件会自动触发。我现在正在检查 DocumentEvent。
  • Mubashir 是您的 TableCellListener 链接中的线程回复者,他于 2012 年 3 月 19 日回复
  • 这正是 OP 不想要的。这只会在编辑停止时触发事件。
猜你喜欢
  • 2013-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
  • 2010-12-19
  • 1970-01-01
  • 2012-04-21
  • 2019-04-30
相关资源
最近更新 更多