Java Swing TableModel handle mode changed event

Description

Java Swing TableModel handle mode changed event


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;
import javax.swing.table.TableModel;

public class Main {
   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);
      TableModel model = table.getModel();
      model.addTableModelListener(new TableModelListener() {
         public void tableChanged(TableModelEvent e) {
            System.out.println(e);
         }/*from   ww  w.  j a va  2 s. c o  m*/
      });

      table.setValueAt("", 0, 0);
      JFrame frame = new JFrame("table model listener");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      frame.add(scrollPane, BorderLayout.CENTER);

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

   }
}



PreviousNext

Related