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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:37:31+00:00 2026-05-24T00:37:31+00:00

In the following code I’ve created a JTable in class main and I’m starting

  • 0

In the following code I’ve created a JTable in class main and I’m starting a thread to collect some value. I want that value to be updated in the JTable shown in the window(i.e. the JTable of class main). Actually I want “a” in the at (0,0) in the following code to change to “new value” at (0,0) after I run this code.

Please Help.

Test.java

package test;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;


public class Test implements ActionListener {
Thread t;
JTable table;
JScrollPane scrollPane;
JButton b;
JFrame frame;
public void body() {
    frame = new JFrame("TableDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new FlowLayout());
    table = new JTable(new MyTableModel());
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);
    scrollPane = new JScrollPane(table);
    frame.add(scrollPane);
    b = new JButton("OK");
    frame.add(b);
    thread a = new thread(new MyTableModel());
    t = new Thread(a);
    frame.pack();
    frame.setVisible(true);
    b.addActionListener(this);
}

public static void main(String[] args) {
    new Test().body();
}

@Override
public void actionPerformed(ActionEvent e) {
    t.start();
}
}

class MyTableModel extends AbstractTableModel
{
    private String[] columnNames = {"First Name",
                                    "Last Name",
                                    "Sport"};
    private Object[][] data = {
    {"a", "a","a"}
    };

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

   public Object getValueAt(int row, int col) {
       return data[row][col];
   }
   public Class getColumnClass(int c) {
       return getValueAt(0, c).getClass();
   }
   public boolean isCellEditable(int row, int col) {
       if (col < 3) {
           return false;
       } else {
           return true;
       }
   }
   public void setValueAt(Object value, int row, int col) {

        data[row][col] = value;
       fireTableCellUpdated(row, col);
    }
}

thread.java

package test;

public class thread implements Runnable {
    MyTableModel model;
    thread(MyTableModel model)
    {
        this.model = model;

    }

    @Override
    public void run() {
    Object aa = "new value";
    this.model.setValueAt(aa, 0, 0);
    System.out.println(this.model.getValueAt(0, 0));
    }       
}
  • 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-24T00:37:32+00:00Added an answer on May 24, 2026 at 12:37 am

    First, you should pass to the thread constructor the tableModel that is attached to the grid, not a new one:

    MyTableModel model = new MyTableModel();
    table = new JTable(model);
    ...
    thread a = new thread(model);
    t = new Thread(a);
    

    And Swing components are not thread-safe, so their use must always be done by the event dispatch thread. To update the cell, the thread must do something like this:

    final Object aa = "new value";
    SwingUtilites.invokeLater(new Runnable() {
        public void run() {
            model.setValueAt(aa, 0, 0);
            System.out.println(model.getValueAt(0, 0));
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following code is in my c++ class static const QString ALARM_ERROR_IMAGE ; i want
Following code does compile whereas name aNumber is not declared before use. class A
the following code compiles fine under gcc: class vec3 { private: float data[3]; public:
The following code causes an error: <c:set var=test value=test1/> <% String resp = abc;
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
The following code works great in IE, but not in FF or Safari. I
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
The following code illustrates an object literal being assigned, but with no semicolon afterwards:
The following code should find the appropriate project tag and remove it from the
The following code is in the /Courses/Detail action: [AcceptVerbs(GET)] public ActionResult Detail(int id) {

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.