栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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


package awtDemo;

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;

@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];
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
 public Class getColumnClass(int c) {
      return getValueAt(0, c).getClass();
    }
    
    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;
      }
    }
    
    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("--------------------------");
    }
  }
  
  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.*;

public class TableCellListener implements PropertyChangeListener, Runnable
{
  private JTable table;
  private Action action;
  private int row;
  private int column;
  private Object oldValue;
  private Object newValue;
  
  public TableCellListener(JTable table, Action action)
  {
    this.table = table;
    this.action = action;
    this.table.addPropertyChangeListener( this );
  }
  
  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;
  }
  
  public int getColumn()
  {
    return column;
  }
  
  public Object getNewValue()
  {
    return newValue;
  }
  
  public Object getOldValue()
  {
    return oldValue;
  }
  
  public int getRow()
  {
    return row;
  }
  
  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();
      }
    }
  }
  
  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 );
  }
  
  @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;
  }
  
  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程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/143225.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号