Listening to JTable Events with a TableModelListener : TableModelListener « Swing Event « Java Tutorial






If you want to dynamically update your table data, you can work with a TableModelListener to find out when the data changes. The interface consists of one method that tells you when the table data changes.

public interface TableModelListener extends EventListener {
  public void tableChanged(TableModelEvent e);
}
Listening to JTable Events with a TableModelListener
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

public class TableModelListenerDemo {
  public static void main(String args[]) {

    final Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } };
    final String columnNames[] = { "#", "English", "Roman" };

    final JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);
    table.getModel().addTableModelListener(new TableModelListener() {

      public void tableChanged(TableModelEvent e) {
         System.out.println(e);
      }
    });

    table.setValueAt("",0,0);
    JFrame frame = new JFrame("Resizing Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);

  }
}








15.35.TableModelListener
15.35.1.Listening to JTable Events with a TableModelListenerListening to JTable Events with a TableModelListener