简体   繁体   中英

Remove AbstractAction from jtable

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.

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.

为什么不简单地使TableCellListener,tcl,一个类字段,如果重建模型则将其删除,然后重新构建侦听器并重新添加它。

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM