1

Overview

I tried to create a custom cell editor for a project I'm working on and what I need to achieve is to fire the table model changed event when an editing is done in my Cell Editor. I have used a custom cell editor and a custom Cell Renderer too with a JXTable.

The Problem

I tried to bind a TableModelListener to the table but nothing happens when I change the values. I have also attached a selection changed listener to fire edit mode for the table cell.

What I need to achieve.

I need to get the line System.out.println("ob = " + ob.toString()); in the TableModelListener executed so I can do whatever I want when the table content changes.

Here is the complete code for my problem.

import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

public class RendererTest extends javax.swing.JFrame {

    public RendererTest() {
        initComponents();

        testresults_editor_grid.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                int col = testresults_editor_grid.getSelectedColumn();
                int row = testresults_editor_grid.getSelectedRow();
                if (row > -1) {
                    if (testresults_editor_grid.isCellEditable(row, col)) {
                        testresults_editor_grid.editCellAt(row, col);
                        testresults_editor_grid.setSurrendersFocusOnKeystroke(true);
                        testresults_editor_grid.getEditorComponent().requestFocusInWindow();
                        ((JTextField) testresults_editor_grid.getEditorComponent()).selectAll();
                    }
                }
            }
        });
        testresults_editor_grid.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        testresults_editor_grid.setRowHeight(25);

        testresults_editor_grid.getModel().addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                if (e.getType() == TableModelEvent.UPDATE) {
                    int c = e.getColumn();
                    int r = e.getFirstRow();
                    Object ob = testresults_editor_grid.getValueAt(r, c);
                    System.out.println("ob = " + ob.toString());
                }
            }
        });
    }

    private void fillTable() {
        Object[] cols = {"DESCRIPTION", "RESULTS(Editable)", "REF. RANGE"};
        Object[][] data = {
            {"ASHAN", 0, "60-100"},
            {"B", 0, "1000-4000"},
            {"C", 0, "10000-60000"}
        };

        DefaultTableModel def = new DefaultTableModel(data, cols) {
            Boolean[] editable = {};
            Boolean[] canEdit = {false, true, false};

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        };
        testresults_editor_grid.setModel(def);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        testresults_editor_grid = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.BorderLayout(0, 5));

        testresults_editor_grid.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        testresults_editor_grid.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(testresults_editor_grid);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        jButton1.setText("Fill Table");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        fillTable();
    }                                        

    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RendererTest().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable testresults_editor_grid;
    // End of variables declaration                   
}

If anyone can help me regarding this issue, or direct me in the right direction, it is really very much appreciated.

Thanks in advance.

Update

I corrected the issue my self. What I missed was I forgot re-attach the listener for TableModel after I attach a new model object to the table.

  • 1
    It's not clear for me what you want to achieve. Please explain it and write a [SSCCE](http://sscce.org) – Sergiy Medvynskyy Oct 07 '16 at 06:52
  • Another question why you don't use the `DefaultCellEditor`??? – Sergiy Medvynskyy Oct 07 '16 at 06:54
  • try to add fireTableCellUpdated(row, col) after you set value in TestResultTableCellEditor – Viet Oct 07 '16 at 07:20
  • @SergiyMedvynskyy: Sir, thank you for your reply. What I need to achive is do something when the cell is finished editing in my above highlighted code. For example: when I put value 10 and use arrow key to move to the cell below, I want to call an another method. I tried using a `DefaultCellEditor` too. The issue exists same. Thanks again for your reply. – Isuru Ranawaka Oct 07 '16 at 07:21
  • Please create [SSCCE](http://sscce.org), so I can reproduce your wrong behavior. – Sergiy Medvynskyy Oct 07 '16 at 07:39
  • @SergiyMedvynskyy: Sir, Thank you again for your support. I corrected the issue my self. What I missed was, I forgot re-attach the listener for TableModel after I attach a new model object to the table. The issue is now solved. – Isuru Ranawaka Oct 07 '16 at 08:26

0 Answers0