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 6111665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:39:24+00:00 2026-05-23T14:39:24+00:00

I’ve faced an issue in adding TableModelListener to AbstractTableModel . The program stops working

  • 0

I’ve faced an issue in adding TableModelListener to AbstractTableModel. The program stops working and the JFrame doesn’t response for any button-clicking or even closing the JFrame.

What I want is to make a JButton enabled iff the number of rows in myTable is equal or more than 2 rows.

Here is my code …

My custom Table:

public class MyTable extends JPanel
{
    public Main main;
    public ArrayList<MyData> lstData;
    public JTable table;
    public MyTableModel model;
    // ...

    public MyTable(ArrayList<MyData> lstData, Main main)
    {
        this.lstData = lstData;
        this.main = main;
        model = new MyTableModel(lstData);
        table = new JTable(model);
        // ...
    }

    // ...

    public int getTableSize()
    {
        return model.getRowCount();
    }
    public TableModel getModel()
    {
        return model;
    }

    public class MyTableModel extends AbstractTableModel
    {  
        protected String[] columnNames = new String[ ] {"#","Name", "Phone Number"};
        protected ArrayList<MyData> lstData;
        protected Class[] types = new Class[]{String.class, String.class, String.class};

        public MyTableModel(ArrayList<MyData> lstData)
        {    this.lstData = lstData;    }

        public void SetData(ArrayList<MyData> lstData)
        {    this.lstData = lstData; fireTableDataChanged();    }

        @Override
        public String getColumnName(int columnIndex)
        {    return columnNames[columnIndex];   }

        @Override
        public Class getColumnClass(int columnIndex)
        {    return types[columnIndex];   }

        public Object getValueAt(int row, int column)
        {
            if (row < 0 || row > lstData.size()) return null;
            MyData obj = lstData.get(row);
            switch(column)
            {
                case 0: return obj.getID();
                case 1: return obj.getName();
                case 2: return obj.getPhoneNumber();
                default: return null;
            }
        }

        public int getRowCount() {  return lstData.size();   }
        public int getColumnCount() {  return columnNames.length;   }

    }
}

Main class:

public class Main extends JFrame implements TableModelListener
{
    public static ArrayList<myData> lstData;
    public static MyTable table;
    public static JButton myButton;
    public Main()
    {
        // ...
        table = new MyTable(lstData, this);
        table.getModel().addTableModelListener(this);
        myButton = new JButton();
        myButton.setEnabled(false);
        // ...
    }

    // ...

    public void tableChanged(TableModelEvent e)
    {
       int firstRow = e.getFirstRow();
       int lastRow = e.getLastRow();
       int mColIndex = e.getColumn();

       switch(e.getType())
       {
           case TableModelEvent.INSERT:
           if(table.getTableSize() >= 2) myButton.setEnabled(true);
           else myButton.setEnabled(false);
           break;

           case TableModelEvent.DELETE:
           if(table.getTableSize() >= 2) myButton.setEnabled(true);
           else myButton.setEnabled(false);
           break;
       }
    }
}

Could you help me to solve this issue? Thanks in advance.

EDIT:

The GUI stop responding only if I add or delete elements from the table.

EDIT2:

No errors or exceptions are thrown after I add elements to the table, it’s just freezing the gui with no response

  • 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-23T14:39:25+00:00Added an answer on May 23, 2026 at 2:39 pm

    Thank you guys for your help, I solve this issue by modifying the tableChanged method:

    public void tableChanged(TableModelEvent e)
    {
       if(table.getTableSize() >= 2) myButton.setEnabled(true);
           else myButton.setEnabled(false);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from

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.