Java Swing实现JTable检测单元格数据变更事件的方法示例

本文实例讲述了Java Swing实现JTable检测单元格数据变更事件的方法。分享给大家供大家参考,具体如下:

在JTable的初级教程中往往会提到,使用TableModel的 addTableModelListener方法可以监听单元格数据的变更,在其事件处理函,数tableChanged中,可以通过e.getColumn()e.getFirstRow()e.getLastRow()e.getType()来获取变更发生的位置和变更的类型(插入、更新或删除)。然而该方法存在2个致命的问题:

1.双击单元格使其处于可编辑状态后,即使没有做出任何修改,当单元格失去焦点时,该事件将被激活。

2.通过该事件你可以获取单元格最新的数据,却无法获取原有数据。

经过一番搜索发现该文章已经解决了这个问题Table Cell Listener,作者自己实现了一个单元格监听器TableCellListener,它订阅了指定table的addPropertyChangeListener,根据e.getPropertyName()来识别单元格编辑事件,根据table.isEditing()方法来判断单元格正在编辑还是编辑完毕。如果是正在编辑,则记录单元格位置和原因数据;如果已经编辑完毕,则记录新数据并与原有数据进行比对,如果不一致则说明单元格数据发生了变更,则激活指定响应函数。

测试用例如下:

TableDemo.java

/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 *  - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 *  - Neither the name of Oracle or the names of its
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package awtDemo;
/*
 * TableDemo.java requires no other files.
 */
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
/**
 * TableDemo is just like SimpleTableDemo, except that it
 * uses a custom TableModel.
 */
@SuppressWarnings("serial")
public class TableDemo extends JPanel {
  private boolean DEBUG = false;
  @SuppressWarnings("unused")
 public TableDemo() {
    super(new GridLayout(1,0));
    JTable table = new JTable(new MyTableModel());
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);
    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);
    //Add the scroll pane to this panel.
    add(scrollPane);
    Action action = new AbstractAction()
    {
      public void actionPerformed(ActionEvent e)
      {
        TableCellListener tcl = (TableCellListener)e.getSource();
        System.out.printf("cell changed%n");
        System.out.println("Row  : " + tcl.getRow());
        System.out.println("Column: " + tcl.getColumn());
        System.out.println("Old  : " + tcl.getOldValue());
        System.out.println("New  : " + tcl.getNewValue());
      }
    };
    TableCellListener tcl = new TableCellListener(table, action);
  }
  class MyTableModel extends AbstractTableModel {
    private String[] columnNames = {"First Name",
                    "Last Name",
                    "Sport",
                    "# of Years",
                    "Vegetarian"};
    private Object[][] data = {
    {"Kathy", "Smith",
     "Snowboarding", new Integer(5), new Boolean(false)},
    {"John", "Doe",
     "Rowing", new Integer(3), new Boolean(true)},
    {"Sue", "Black",
     "Knitting", new Integer(2), new Boolean(false)},
    {"Jane", "White",
     "Speed reading", new Integer(20), new Boolean(true)},
    {"Joe", "Brown",
     "Pool", new Integer(10), new Boolean(false)}
    };
    public int getColumnCount() {
      return columnNames.length;
    }
    public int getRowCount() {
      return data.length;
    }
    public String getColumnName(int col) {
      return columnNames[col];
    }
    public Object getValueAt(int row, int col) {
      return data[row][col];
    }
    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell. If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
 public Class getColumnClass(int c) {
      return getValueAt(0, c).getClass();
    }
    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
      //Note that the data/cell address is constant,
      //no matter where the cell appears onscreen.
      if (col < 2) {
        return false;
      } else {
        return true;
      }
    }
    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {
      if (DEBUG) {
        System.out.println("Setting value at " + row + "," + col
                  + " to " + value
                  + " (an instance of "
                  + value.getClass() + ")");
      }
      data[row][col] = value;
      fireTableCellUpdated(row, col);
      if (DEBUG) {
        System.out.println("New value of data:");
        printDebugData();
      }
    }
    private void printDebugData() {
      int numRows = getRowCount();
      int numCols = getColumnCount();
      for (int i=0; i < numRows; i++) {
        System.out.print("  row " + i + ":");
        for (int j=0; j < numCols; j++) {
          System.out.print(" " + data[i][j]);
        }
        System.out.println();
      }
      System.out.println("--------------------------");
    }
  }
  /**
   * Create the GUI and show it. For thread safety,
   * this method should be invoked from the
   * event-dispatching thread.
   */
  private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("TableDemo - www.jb51.net");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Create and set up the content pane.
    TableDemo newContentPane = new TableDemo();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);
    //Display the window.
    frame.pack();
    frame.setVisible(true);
  }
  public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }
}

TableCellListener.java

package awtDemo;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
/*
 * This class listens for changes made to the data in the table via the
 * TableCellEditor. When editing is started, the value of the cell is saved
 * When editing is stopped the new value is saved. When the oold and new
 * values are different, then the provided Action is invoked.
 *
 * The source of the Action is a TableCellListener instance.
 */
public class TableCellListener implements PropertyChangeListener, Runnable
{
  private JTable table;
  private Action action;
  private int row;
  private int column;
  private Object oldValue;
  private Object newValue;
  /**
   * Create a TableCellListener.
   *
   * @param table  the table to be monitored for data changes
   * @param action the Action to invoke when cell data is changed
   */
  public TableCellListener(JTable table, Action action)
  {
    this.table = table;
    this.action = action;
    this.table.addPropertyChangeListener( this );
  }
  /**
   * Create a TableCellListener with a copy of all the data relevant to
   * the change of data for a given cell.
   *
   * @param row the row of the changed cell
   * @param column the column of the changed cell
   * @param oldValue the old data of the changed cell
   * @param newValue the new data of the changed cell
   */
  private TableCellListener(JTable table, int row, int column, Object oldValue, Object newValue)
  {
    this.table = table;
    this.row = row;
    this.column = column;
    this.oldValue = oldValue;
    this.newValue = newValue;
  }
  /**
   * Get the column that was last edited
   *
   * @return the column that was edited
   */
  public int getColumn()
  {
    return column;
  }
  /**
   * Get the new value in the cell
   *
   * @return the new value in the cell
   */
  public Object getNewValue()
  {
    return newValue;
  }
  /**
   * Get the old value of the cell
   *
   * @return the old value of the cell
   */
  public Object getOldValue()
  {
    return oldValue;
  }
  /**
   * Get the row that was last edited
   *
   * @return the row that was edited
   */
  public int getRow()
  {
    return row;
  }
  /**
   * Get the table of the cell that was changed
   *
   * @return the table of the cell that was changed
   */
  public JTable getTable()
  {
    return table;
  }
//
// Implement the PropertyChangeListener interface
//
  @Override
  public void propertyChange(PropertyChangeEvent e)
  {
    // A cell has started/stopped editing
    if ("tableCellEditor".equals(e.getPropertyName()))
    {
      if (table.isEditing()){
        //System.out.printf("tableCellEditor is editing..%n");
        processEditingStarted();
      }
      else{
        //System.out.printf("tableCellEditor editing stopped..%n");
        processEditingStopped();
      }
    }
  }
  /*
   * Save information of the cell about to be edited
   */
  private void processEditingStarted()
  {
    // The invokeLater is necessary because the editing row and editing
    // column of the table have not been set when the "tableCellEditor"
    // PropertyChangeEvent is fired.
    // This results in the "run" method being invoked
    SwingUtilities.invokeLater( this );
  }
  /*
   * See above.
   */
  @Override
  public void run()
  {
    row = table.convertRowIndexToModel( table.getEditingRow() );
    column = table.convertColumnIndexToModel( table.getEditingColumn() );
    oldValue = table.getModel().getValueAt(row, column);
    //这里应对oldValue为null的情况做处理,否则将导致原值与新值均为空时仍被视为值改变
    if(oldValue == null)
      oldValue = "";
    newValue = null;
  }
  /*
   *  Update the Cell history when necessary
   */
  private void processEditingStopped()
  {
    newValue = table.getModel().getValueAt(row, column);
    //这里应对newValue为null的情况做处理,否则后面会抛出异常
    if(newValue == null)
      newValue = "";
    // The data has changed, invoke the supplied Action
    if (! newValue.equals(oldValue))
    {
      // Make a copy of the data in case another cell starts editing
      // while processing this change
      TableCellListener tcl = new TableCellListener(
        getTable(), getRow(), getColumn(), getOldValue(), getNewValue());
      ActionEvent event = new ActionEvent(
        tcl,
        ActionEvent.ACTION_PERFORMED,
        "");
      action.actionPerformed(event);
    }
  }
}

运行效果:

由图可见,单元格数据修改后,控制台输出内容变更信息!

更多关于java相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java字符与字符串操作技巧总结》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

(0)

相关推荐

  • Java Swing组件下拉菜单控件JComboBox用法示例

    本文实例讲述了Java Swing组件下拉菜单控件JComboBox用法.分享给大家供大家参考,具体如下: JComboBox是Swing中的下拉菜单控件.它永远只能选中一个项目,然而比单选按钮节省空间.如果使用setEditable设置为true则内部选项的文本可以编辑,因此这种组件被称为组合框.注意,对选项的编辑只会影响当前项,而不会改变列表内容.可以使用addItem方法来添加选项列表,或者使用insertItemAt在任何位置插入选项:然而如果有大量选项需要添加,这种方法是非常笨重的,可

  • Java Swing中JTable渲染器与编辑器用法示例

    本文实例讲述了Java Swing中JTable渲染器与编辑器用法.分享给大家供大家参考,具体如下: JTable的内容.外观.事件响应在很大程度上是由渲染器与编辑器控制的.具体说来,渲染器负责单元格的外观比如前景色.背景色,以及单元格提示:编辑器负责单元格的内容和事件响应.编辑器默认为文本框形式,也可以使用下拉菜单.按钮.单选按钮等形式.下面通过一个demo程序来说明渲染器与编辑器的基本用法. JButtonTableExample.java package awtDemo; import j

  • Java Swing中的下拉式菜单(menu)、弹出式菜单(JPopupMenu)、选项卡窗体(JTabbedPane)组件使用案例

    菜单是GUI中最常用的组件,菜单不是Component类的子类,不能放置在普通容器中,不受布局管理器的约束,只能放置在菜单栏中. 菜单组件由菜单栏 (MenuBar).菜单(Menu)和菜单项(MenuItem)三部分组成. 一个菜单栏由若干个菜单组成,一个菜单又由若干个菜单项组成.一般菜单栏放 Frame 窗口中,只要调用 Frame 类的 setMenuBar()方法即可. 常用的菜单有:下拉式菜单和弹出式菜单(独立显示,可出现在任意地方). 一:下拉式菜单的创建步骤: 1.创建一个菜单栏.

  • Java Swing中JList选择事件监听器ListSelectionListener用法示例

    本文实例讲述了Java Swing中JList选择事件监听器ListSelectionListener用法.分享给大家供大家参考,具体如下: 当JList中的元素被选中时,选择事件将被触发.对于JTable也是一样,你可以把它看做是多个并列的JList.那么,如果程序需要对该事件做出响应,需要以下步骤: (1)创建一个实现了 ListSelectionListener的监听器: (2)使用JList或selectionModel的addListSelectionListener添加监听器: (3

  • java之swing下拉菜单实现方法

    本文实例讲述了java之swing下拉菜单实现方法.分享给大家供大家参考.具体如下: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class test extends JApplet implements ItemListener{ JLabel jtf; ImageIcon a1, a2, a3; public void init(){ Container contentPane = getC

  • Java Swing组件布局管理器之FlowLayout(流式布局)入门教程

    本文实例讲述了Java Swing组件布局管理器之FlowLayout(流式布局).分享给大家供大家参考,具体如下: FlowLayout应该是Swing布局管理器学习中最简单.最基础的一个.所谓流式,就是内部控件像水流一样,从前到后按顺序水平排列,直到达到容器的宽度时跳转到第二行.既然是水平排列,那么就存在三种基本的对齐方式:居中对齐(CENTER ).左对齐(LEFT )和右对齐(RIGHT ).然而,FlowLayout还提供两种对齐方式:LEADING,表示控件与容器方向开始边对应:TR

  • Java Swing中的表格(JTable)和树(JTree)组件使用实例

    一:表格(JTable): 1.基本概念: 表格(JTable)是Swing 新增加的组件,主要是为了将数据以表格的形式显示.给显示大块数据提供了简单的机制. 2.常用构造方法: * JTable():使用系统默认的模型创建一个JTable 实例.  * JTable(int numRows,int numColumns):创建一个使用DefaultTableModel 指定行.列的空表格.  * JTable(Object[ ][ ] rowData,Object[ ][ ] columnNa

  • Java Swing中的文本框(JTextField)与文本区(JTextArea)使用实例

    一:JTextField的(文本框)使用: JTextField 是一个轻量级组件,它允许编辑单行文本. 1.JTextField的常用构造方法: JTextField() 构造一个新的 TextField. JTextField(int columns) 构造一个具有指定列数的新的空 TextField. JTextField(String text) 构造一个用指定文本初始化的新TextField. JTextField(String text, int columns) 构造一个用指定文本

  • Java Swing中的JButton、JComboBox、JList和JColorChooser组件使用案例

    Swing 的组件与AWT 组件相似,但又为每一个组件增添了新的方法,并提供了更多的高级组件. Swing 的基本组件: 1.按钮(JButton): Swing 中的按钮可以显示图像,并且可以将按钮设置为窗口的默认图标,而且还可以将多个图像指定给一个按钮. (1).JButton 常用的构造方法. JButton(String text):按钮上显示字符. JButton(Icon icon) :按钮上显示图标. JButton(String text, Icon icon):按钮上既显示图标

  • Java Swing中JDialog实现用户登陆UI示例

    本文实例讲述了Java Swing中JDialog实现用户登陆UI.分享给大家供大家参考,具体如下: JDialog是一种对话框组件,它常常与JOptionPane配合使用.JOptionPane提供对话框内部的消息.按钮等内容,JDialog提供对话框窗体,提供模态/非模态等属性.JDialog与JFrame在外观上的区别在于,它没有最大化/最小化按钮.如下图所示: JDialog窗体 点击File->connect之后弹出登陆窗口: JFrame窗体 在下面的demo中,演示了JDialog

  • Java Swing组件JFileChooser用法实例分析

    本文实例讲述了Java Swing组件JFileChooser用法.分享给大家供大家参考,具体如下: JFileChooser文件选择器是Swing中经常用到的一个控件.它的使用主要包含以下几个参数: 1.当前路径.也就是它第一次打开时所在的路径,许多软件喜欢设置为桌面. 2.文件过滤器.通过设置文件过滤器,只有特定类型的文件是可见的,比如文本,音频,等. 3.选择模式.包含三种情况:仅文件, 仅目录, 文件或目录. 4.是否允许多选. JFileChooser的参数当然不止以上4种,它还有很多

随机推荐