Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6981215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:07:39+00:00 2026-05-27T18:07:39+00:00

Ok. I have a large project where a particular jtable is created at startup

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:07:39+00:00Added an answer on May 27, 2026 at 6:07 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large project for which I am attempting to use TDD. I
I have a large project where we have 2-3 dll projects that are converted
I have a large project but I need only some folders in my svn
I have a large project in Vb.net which drives a web browser control around.
I have a large GUI project that I'd like to port to Linux. What
I have this large C++ project that I need to build on a platform
I have a rather large project which contains a number of third-party dependencies which
I have a rather large project developed on Sharepoint and Project Server, designed as
Say you have a large PHP project and suddenly, when attempting to run it,
Environment: Team Foundation Server 2005 Visual Studio 2008 I have a reasonably large project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.