Categories

Simple CellEditorListener example in Java

JFrameThis is an easy example on how to detect changes to data in a JTable.

The example will run by itself. Just copy the code to a file and name it CellEditorListenerExample and it is ready to be compiled and run.

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;

/**
 * Simple example on CellEditorListener. Class extends CellEditorListener to be able
 * to be notified about changes in data in a cell in JTable.
 *
 * This example is built on my JFrame/JTable example.
 *
 * @author http://www.gammelsaeter.com/
 */

public class CellEditorListenerExample implements CellEditorListener {

    private String[] _titles = new String[] {"Name", "Vage"};
    private String[][] _data = new String[][] {{"Donald Duck", "100"},{"Mickey Mouse", "130"}};

    public CellEditorListenerExample() {
        // Make the frame
        JFrame frame = new JFrame("CellEditorListener example");
        frame.setSize(300, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Action when window closes

        JTable table = new JTable(_data, _titles);            // Set up the table
        JScrollPane scrollPane = new JScrollPane(table);      // Set table in a scroll pane

        // To enable listening for data changes in cells in a JTable, we need to tell
        // every cell editor about the CellEditorListener. In this example we only use
        // default cell editor for cells of type String. If you have other editors, you
        // call the editors addCellEditorListener function and give it your listener.
        table.getDefaultEditor(String.class).addCellEditorListener(this);

        // Add scrollpane with table to the frame and show the frame to user
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.setVisible(true);
    }

    /**
     * Listens for cells that has been edited. When a cell has been edited, this
     * function will run.
     */

    public void editingStopped(ChangeEvent e) {
        System.out.println("A cell has been edited.");
    }

    /**
     * Listens for cells where editing has been canceled (cell data has not been
     * changed).
     */

    public void editingCanceled(ChangeEvent e) {
        System.out.println("Editing of a cell has been canceled.");
    }

    public static void main(String args[]) {
        CellEditorListenerExample frame = new CellEditorListenerExample();
    }

}

4 comments to Simple CellEditorListener example in Java

  • This is quite close to what i wanted.

  • Enrique

    This example really helped me; thank you for posting it! there are very few examples of how to use CellEditorListeners, and yours is the best I’ve found by far.

  • GC

    Thank you very much. this helped me out alot.
    =)

  • Andy

    I ran your code in Eclipse and the method “editingCanceled” doesn’t seem to work. i’ve been searching the web for 2 days now for a reason to this.
    maybe someone reading this could help me out here.

    thanks

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>