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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:38:15+00:00 2026-06-09T22:38:15+00:00

I have a multi threaded program and I’m trying to use a JTable to

  • 0

I have a multi threaded program and I’m trying to use a JTable to view the progress of each thread. Each thread is a row the JTable. I have posted a simplified and relevant code of what I have now. Each time I press start a new thread is started and the row is added to the JTable. But how do I update the “Column 2” which of the row which belongs to that thread from the running thread?

In main this is what I have

JTable table = new JTable(new DefaultTableModel(new Object[]{"Thread Name", "Progress"}, 0));

btnBegin.addActionListener(new ActionListener()
{
  public void actionPerformed(ActionEvent arg0)
  {
    Thread newThread = new Thread(new MyThreadClass(country, category));
    newThread.start();
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    model.addRow(new Object[]{"Thread " + threadNumber, "Column 2"});
  }
});
  • 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-06-09T22:38:16+00:00Added an answer on June 9, 2026 at 10:38 pm

    If you can add a field tableModel to MyThreadClass (field+constructor argument), from this class, you can invoke the code @sedran was mentioning.

    Example:

    public class T {
    
    static int threadNumber = 0;
    
    public static void main(String[] args) throws Exception {
    
        final JTable table = new JTable(new DefaultTableModel(new Object[] { "Thread Name", "Progress" }, 0));
    
        JButton btnBegin = new JButton("Begin");
        btnBegin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                DefaultTableModel model = (DefaultTableModel) table.getModel();
                MyThreadClass newThread = new MyThreadClass(model, threadNumber++);
                newThread.start();
    
                model.addRow(new Object[] { "Thread " + threadNumber, "Column 2" });
            }
        });
    
        JFrame frame = new JFrame();
        frame.add(btnBegin, BorderLayout.NORTH);
        frame.add(table);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }
    
    static class MyThreadClass extends Thread {
        private final DefaultTableModel model;
        private final int threadNumber;
    
        public MyThreadClass(DefaultTableModel model, int threadNumber) {
            super();
            this.model = model;
            this.threadNumber = threadNumber;
        }
    
        @Override
        public void run() {
            for (int i = 0; i <= 5; i++) {
                final int index = i;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                SwingUtilities.invokeLater(new Runnable() {
    
                    @Override
                    public void run() {
                        model.setValueAt(index * 20, threadNumber, 1);
                    }
                });
            }
        }
    }
    

    }

    Please note that the use of SwingUtilities.invokeLater is important as the view MUST be refreshed in the EDT.

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

Sidebar

Related Questions

I have implemented a multi-threaded program which involves spawning a thread for each user,and
Possible Duplicate: Is stl vector concurrent read thread-safe? I have a multi-threaded program that
I have a multi-threaded program, where I have one thread to watch over several
I have a multi-threaded Cocoa app that processes images. The program has a progress
I have a multi-threaded program. I want to embed a python interpreter. I don't
I have a multi-threaded program, and have been asked to add in an option
I currently have a multi-threaded program that crawls websites and writes their text to
I have an existing multi-threaded C++ program which uses a pool of sockets for
When I create a multi-threaded program and I use methods such as Wait or
I have a simple multi-threaded program, with the following structure. public void someFunction() {

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.