簡體   English   中英

從jtable中刪除AbstractAction

[英]Remove AbstractAction from jtable

好。 我有一個大型項目,在啟動時會創建一個特定的jtable,並且永遠不會對其進行重建。 將根據各種用戶操作刷新表模型並重繪表。

我添加了一個自定義TableCellListener類,以對單元格更改以及AbstractAction做出反應。 這是第一次在表中填充數據時執行的代碼。 (沒有“ firstLoad”檢查,每次重繪表時都會附加多個操作)。

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是Rob Camick在此處發布的自定義偵聽器,“ sayIt”位是我自己的調試代碼。

這一切都很好,但是我想在每次重建表時完全刪除偵聽器,然后再次添加它,因為它正在“記住”上次選擇的單元格中的值,由於表數據是新的,因此現在無效。

我相當確定'removePropertyChangeListener()'類型的調用可以做到,但它希望將偵聽器作為參數,而且我不確定如何找到它。

因為它正在“記住”上次選擇的單元格中的值,由於表數據是新數據,因此該值現在無效。

開始編輯時應保存當前值,停止編輯時應生成事件。 更改TableModel時,不應編輯任何單元格。 因此,當您生成表示您選擇並開始在其他單元格上進行編輯的下一個事件時,在這種情況下,您應該具有新模型的當前值。 這對我來說可以:

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

如果需要更多幫助,請發布SSCCE。

為什么不簡單地使TableCellListener,tcl,一個類字段,如果重建模型則將其刪除,然后重新構建偵聽器並重新添加它。

只要記住實例變量中的TableCellListener實例即可:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM