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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:22:08+00:00 2026-06-17T08:22:08+00:00

I have a runnable gui swing class that sets my variables when the constructor

  • 0

I have a runnable gui swing class that sets my variables when the constructor is initialized. When the gui runs, it runs as its own seperate thread. The problem however is when an action-event is triggered on my gui, when I try to access my initialized variables they are reset to their default. After some debugging, it seems that the action-event triggered begins as a thread of its own. How can I access the correct variables in the right thread when processing my action events?

Example code:

public class myGui implements Runnable{
    private flag = false;
    public myGui(){
        flag = true;
    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    System.out.println("The value of flag is: " + flag); // prints flag is false
    }
    public void run(){
        // Do stuff
    }
    // More code ...
}

In my example, when initializing the constructor in the thread, flag is set to true. However when the action-event is triggered it will see flag as set to false ignoring my threads variables. How do i fix this?

Thanks

  • 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-17T08:22:09+00:00Added an answer on June 17, 2026 at 8:22 am

    The crucial part here is visibility of your variables across different threads. In the absence of synchronization, the compiler, processor, and runtime can do some downright weird things to the order in which operations appear to execute.

    Hence, use the volatile modifier on your flag variable to ensure that updates to a variable are propagated predictably to other threads.

    Take a look at the Initial Threads section of Concurrency in Swing. Make sure that UI updates are run in the Event Dispatcher Thread for instance with invokeLater or invokeAndWait. This should give you are clearer understanding of the different threads involved in Swing.

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

Sidebar

Related Questions

In Java, how can I have a Runnable class that, depending on its constructor,
I have a Swing-GUI and an external class. In the constructor of the Swing
I have a runnable jar (swing GUI) that uses a lot of resources (Videos,
I have a Runnable class that I'm writing. Inside of it, I have two
I have the following code in a Runnable that gets passed to a thread
I have a handler and a runnable, Handler has a loop that runs from
My program consists of a simple gui and a class that extends thread! i
I have a main class Gui that handles my GUI and calls a few
I have a thread like this public class SMS { class Read implements Runnable
I have this Java Swing application that starts a new thread that uses 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.