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

  • Home
  • SEARCH
  • 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 9227811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:10:18+00:00 2026-06-18T05:10:18+00:00

I have 2 classes .java The main : public class Controller extends javax.swing.JFrame {

  • 0

I have 2 classes .java

The main :

public class Controller extends javax.swing.JFrame
{   
    public static void updateProgressBar(int i) {
        jProgressBar1.setValue(i);
        jProgressBar1.repaint();
    }

    public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Controller app = new Controller();
                app.setVisible(true);
                app.setResizable(false);
            }
        });
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        ChildModel model = new ChildModel();
        Thread t1 = new Thread(model);
        t1.start();
    }

    private javax.swing.JProgressBar jProgressBar1; //Initialized with Netbeans builder
}

My ChildModel (ChildModel.java) computes some code (that takes around 10-20 sec) and I want to show the progress on the father class (Controller.java).

Here is my ChildModel :

public class ChildModel implements Runnable
{
    public ChildModel(){ /* Something */ }

    public void complexMath()
    {
        //Lots of logic here
        Controller.updateProgression(purcent);
    }

    @Override
    public void run() {
        complexMath();
    }
}

The problem is obviously my static void updateProgressBar that cannot modify a non-static variable. How can I accomplish this ?

  • 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-18T05:10:20+00:00Added an answer on June 18, 2026 at 5:10 am

    The jProgressBar1 variable is an instance variable, so you can’t access it from a static method. And the method shouldn’t be static: you want to update the progress in the controller, and not in all the Controller instances.

    Pass a reference to the controller to the ChildModel, and use this reference from the ChildModel in order to update the progress bar. Also remember that all Swing interactions must be done in the EDT, and not in a background thread. SO the code should look like this:

    public class Controller extends javax.swing.JFrame
    {   
        public void updateProgressBar(int i) {
            jProgressBar1.setValue(i);
            // no need for repaint. The progress bar knows it must be repainted 
            // when its value changes
        }
    
        public static void main(String args[]) {
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    Controller app = new Controller();
                    app.setVisible(true);
                    app.setResizable(false);
                }
            });
        }
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
            ChildModel model = new ChildModel(this);
            Thread t1 = new Thread(model);
            t1.start();
        }
    
        private javax.swing.JProgressBar jProgressBar1; //Initialized with Netbeans builder
    }
    
    public class ChildModel implements Runnable
    {
    
        private Controller controller;
    
        public ChildModel(Controller controller){
            this.controller = controller;
        }
    
        public void complexMath()
        {
            //Lots of logic here
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    controller.updateProgression(percent);
                }
            });
        }
    
        @Override
        public void run() {
            complexMath();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: For(int i=0; i<4; i++) playSound(Sound.wav); I have the following classes: Main import java.io.IOException;
I have a java application where main class is dependent with some other classes.
I'm looking at some Java classes that have the following form: public abstract class
I am a beginner learning Java. I have two classes, public class myClassA {
I'm about to learn the java threading facility. I have 2 classes: public class
i have three classes : CustomerData.java import java.util.Date; public class CustomerData { private String
I have two java classes. Schedule is the main class that uses an array
I have the following classes: package models; public class Test extends activejdbc.Model { }
I have following classes : Emp.java final public class Emp { private Integer id;
I have 2 classes: Class A: public class A { static B b =

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.