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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:25:00+00:00 2026-06-06T18:25:00+00:00

This question is related somewhat to the one i asked HERE . Now, i

  • 0

This question is related somewhat to the one i asked HERE.
Now, i have a class "Controller" which consists of the main method and all the swing components. there is a class named "VTOL" which consists of a variable named "altitude"(i have declared this variable volatile as of now).

here is a class that consists of a thread which runs in the background:

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Vineet
 */
public class Gravity extends Thread {
    
    String altStr;
    double alt;
    Controller ctrl = new Controller();

    @Override
    public void run() {
        while (true) {
            alt=VTOL.altitude;
            System.out.println(alt);
            alt = alt-0.01;
            VTOL.altitude= (int) alt;
            altStr=new Integer(VTOL.altitude).toString();
            ctrl.lblAltitude.setText(altStr);
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

Firstly, the problem i was facing initially was that i couldnt update value of "altitude" it remained 0 throughout the execution of program. So i declared it as volatile (I dont know if its a good practice)

Secondly, there is a jLabel in Controller class named "lblAltitude", i wish to update its value as its changed in this thread, but somehow thats not happening.
How can i do that?

  • 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-06T18:25:01+00:00Added an answer on June 6, 2026 at 6:25 pm

    A solution is to use a SwingPropertyChangeSupport object, to make altitude a “bound” property with this support object, to have your GUI listener to this model class and to thereby notify the GUI of changes in altitude.

    e.g.,

    import java.beans.PropertyChangeListener;
    import javax.swing.event.SwingPropertyChangeSupport;
    
    public class Gravity implements Runnable {
       public static final String ALTITUDE = "altitude";
       private SwingPropertyChangeSupport swingPcSupport = new SwingPropertyChangeSupport(this);
       private volatile double altitude;
    
       @Override
       public void run() {
          while (true) {
             double temp = altitude + 10;
             setAltitude(temp); // fires the listeners
             try {
                Thread.sleep(10);
             } catch (InterruptedException e) {
                e.printStackTrace();
             }
          }
    
       }
    
       public double getAltitude() {
          return altitude;
       }
    
       public void setAltitude(double altitude) {
          Double oldValue = this.altitude;
          Double newValue = altitude;
    
          this.altitude = newValue;
    
          // this will be fired on the EDT since it is a SwingPropertyChangeSupport object
          swingPcSupport.firePropertyChange(ALTITUDE, oldValue, newValue);
       }
    
       public void addPropertyChangeListener(PropertyChangeListener listener) {
          swingPcSupport.addPropertyChangeListener(listener);
       }
    
       public void removePropertyChangeListener(PropertyChangeListener listener) {
          swingPcSupport.removePropertyChangeListener(listener);
       }
    
    
    }
    

    For a more complete runnable example:

    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import javax.swing.*;
    import javax.swing.event.SwingPropertyChangeSupport;
    
    public class GravityTestGui extends JPanel {
       private static final long ALT_SLEEP_TIME = 400;
       private static final double ALT_DELTA = 5;
       JLabel altitudeLabel = new JLabel("     ");
       private Gravity gravity = new Gravity(ALT_SLEEP_TIME, ALT_DELTA);
    
       public GravityTestGui() {
          add(new JLabel("Altitude:"));
          add(altitudeLabel);
    
          gravity.addPropertyChangeListener(new PropertyChangeListener() {
    
             @Override
             public void propertyChange(PropertyChangeEvent pcEvt) {
                if (Gravity.ALTITUDE.equals(pcEvt.getPropertyName())) {
                   String altText = String.valueOf(gravity.getAltitude());
                   altitudeLabel.setText(altText);
                }
             }
          });
    
          new Thread(gravity).start();
       }
    
       private static void createAndShowGui() {
          GravityTestGui mainPanel = new GravityTestGui();
    
          JFrame frame = new JFrame("GravityTest");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    
    
    }
    
    class Gravity implements Runnable {
       public static final String ALTITUDE = "altitude";
       private SwingPropertyChangeSupport swingPcSupport = new SwingPropertyChangeSupport(this);
       private volatile double altitude;
       private long sleepTime;
       private double delta;
    
       public Gravity(long sleepTime, double delta) {
          this.sleepTime = sleepTime;
          this.delta = delta;
       }
    
       @Override
       public void run() {
          while (true) {
             double temp = altitude + delta;
             setAltitude(temp); // fires the listeners
             try {
                Thread.sleep(sleepTime);
             } catch (InterruptedException e) {
                e.printStackTrace();
             }
          }
    
       }
    
       public double getAltitude() {
          return altitude;
       }
    
       public void setAltitude(double altitude) {
          Double oldValue = this.altitude;
          Double newValue = altitude;
    
          this.altitude = newValue;
    
          // this will be fired on the EDT since it is a SwingPropertyChangeSupport object
          swingPcSupport.firePropertyChange(ALTITUDE, oldValue, newValue);
       }
    
       public void addPropertyChangeListener(PropertyChangeListener listener) {
          swingPcSupport.addPropertyChangeListener(listener);
       }
    
       public void removePropertyChangeListener(PropertyChangeListener listener) {
          swingPcSupport.removePropertyChangeListener(listener);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to that one. But somewhat extended. Let's assume we have
I have a somewhat complicated question related to MySQL. This is the table I
This is somewhat related to my question here . Basically it is trivial to
This is somewhat related to this question , but not quite. I have two
This is somewhat related to this question : I have a table with a
My question is somewhat related to this one: How does a generic constraint prevent
My question is somewhat related to this one: Explicitly implemented interface and generic constraint
This question is somewhat related to this. I want to have document storage along
This question is somewhat related to this question: Is it possible to have a
Although somewhat related to this question , I have what I think is a

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.