1

Ok. I have a large project where a particular jtable is created at startup and never gets rebuilt. The table model is refreshed and the table redrawn based on various user actions.

I've added a custom TableCellListener class to react to cell changes along with an AbstractAction. Here is the code that gets executed the first time the table is populated with data. (Without the 'firstLoad' check, multiple actions were getting attached every time the table was redrawn).

if(firstLoad) {
    AbstractAction action = new AbstractAction()
    {
        public void actionPerformed(ActionEvent e)
        {
            TableCellListener tcl = (TableCellListener)e.getSource();


                    sayIt("Row:" + tcl.getRow()+" Column:" + tcl.getColumn()+
                        " Old:" + tcl.getOldValue()+" New:" + tcl.getNewValue());

        }
    };

    firstLoad = false;
    TableCellListener tcl = new TableCellListener(table2, action);
}

TableCellListener is a custom listener posted here by Rob Camick and the 'sayIt' bit is my own debuging code.

This all works great but I would like to remove the listener completely each time the table is rebuilt and add it in again because it is 'remembering' the value from the last selected cell, which is now not valid because the table data is new.

I'm fairly sure a 'removePropertyChangeListener()' type call would do it but it expects the listener as an argument and I'm not sure how to find it.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
jeff
  • 87
  • 13
  • as @camickr already mentioned: something is wrong with the code you are not showing, time for an sscce – kleopatra Dec 25 '11 at 12:01
  • Yes. I am trouble shooting now. I also use a selection listener to auto select cell contents for editing (keeps the user from having to double click constantly) and I think that that could be the issue as camickr's test code does work as expected. – jeff Dec 26 '11 at 17:37

3 Answers3

4

because it is 'remembering' the value from the last selected cell, which is now not valid because the table data is new.

It should save the current value when you start editing and generate the event when you stop editing. When you change the TableModel you should not be editing any cell. Therefore, when you generate the next event that implies you selected and started editing on a different cell in which case you should have the current value for the new model. It works fine for me:

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableCellListenerTest2
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public static void createAndShowGUI()
    {
        final JTable table = new JTable( TableCellListenerTest2.createModel());
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);

        Action action = new AbstractAction()
        {
            public void actionPerformed(ActionEvent e)
            {
                TableCellListener tcl = (TableCellListener)e.getSource();
                System.out.println( tcl.getOldValue() + " : " + tcl.getNewValue() );
            }
        };

        TableCellListener tcl = new TableCellListener(table, action);

        JButton button = new JButton("Reset Model");
        button.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                table.setModel( TableCellListenerTest2.createModel() );
            }
        });

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Table Cell Listener");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( scrollPane );
        frame.add(button, BorderLayout.SOUTH);
        frame.setSize(400, 160);
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static TableModel createModel()
    {
        Random random = new Random();

        DefaultTableModel model = new DefaultTableModel(10, 2);

        for (int i = 0; i < model.getRowCount(); i++)
            model.setValueAt("" + random.nextInt(100), i, 0);

        return model;
    }
}

Post your SSCCE if you need more help.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Not sure what a SSCCE is. My apologies for being a bit of a newbie. But yes, your code does work as expected. "When you change the TableModel you should not be editing any cell." ... this may be the issue I think. I use a selection listener (as mentioned above) as well so I will most likely need to deselect rows/columns before the table is rebuilt but so far 'table2.getSelectionModel().clearSelection()' compiles but just breaks everything. Will continue to troubleshoot. Thanks for the tip and test code. – jeff Dec 26 '11 at 17:48
  • I posted an SSCCE. You can search the web to find out more details. I don't see why a SelectionLIstener would cause a problem either. To test out your theory all you have to do is modify my code to add a SelectionListener. – camickr Dec 26 '11 at 19:16
  • It seems I had a listener on the selection model AND the column model. Once I removed the column model listener and then, just before re-populating the table, canceled editing and did a clearSelection() it all worked as it should. Thanks for the help and pointing me in the right direction. (And the SSCCE tip). – jeff Dec 30 '11 at 03:43
1

Why not simply make the TableCellListener, tcl, a class field, and remove it if the model is rebuilt, then re-build the listener and re-add it.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Hovercraft: thanks for the fast reply. I'm a bit of a newbie on this so I'll experiment a bit and see how it goes. Thanks. – jeff Dec 25 '11 at 00:32
0

Just remember the TableCellListener instance in an instance variable:

// side effect: the tcl is added as PropertyChangeListener to the table 
// (bad design, IMHO)
this.tcl = new TableCellListener(table2, action);

// when the table data changes: 
if (this.tcl != null) {
    table2.removePropertyChangeListener(this.tcl);
}
this.tcl = new TableCellListener(table2, action);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks JB. Tried it but it doesn't like the 'this.tcl'. tcl needs to be declared first somewhere? I'll see what I can figure out. – jeff Dec 25 '11 at 00:32
  • If it's this.tcl, this means that tcl is indeed an instance variable, which must be declared in the instance. See http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html – JB Nizet Dec 25 '11 at 08:47
  • -1 there is no need to remove the listener, something's wrong with the code the OP isn't showing (as @camickr already mentioned) And nothing bad with the design as well: the whole point of that helper class _is_ to automagically keep track of the value before editing (which implies ... well ... listening to change of the table's editing state) – kleopatra Dec 25 '11 at 11:58
  • My "bad design" remark was about the fact that a constructor has a side effect, and lets the not yet fully constructed instance leak. Just calling "new TableCellListener(...)" to both create the listener, and attach it to a table, is bad design, IMHO. – JB Nizet Dec 25 '11 at 12:41
  • @kleopatra - Yes, it turns out there were some code issues with other areas of the program as mentioned above. After tracking them down the change listener worked perfectly as designed. Thanks to all for the help! – jeff Dec 30 '11 at 04:05