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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:55:44+00:00 2026-05-28T22:55:44+00:00

I am having trouble updating a jlabel in a method. here is my code:

  • 0

I am having trouble updating a jlabel in a method. here is my code:

    JLabel curStatus = new JLabel("");
JButton jbtnSubmit;

public static void main(String[] args) {
    test gui = new test();
    gui.startGUI();
    // gui.setCurStatus("testing!"); << seems to work here, 
    //but when i call it from another class, it doesn't want to run.
}

// Set up the GUI end for the user
public void startGUI() {
    // These are all essential GUI pieces
    new JTextArea("");
    final JFrame jfrm = new JFrame("my program");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(300, 300);
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    jbtnSubmit = new JButton("Submit");

    jfrm.add(jbtnSubmit);
    jfrm.add(curStatus);
    jfrm.setVisible(true);
}

public void setCurStatus(String inCurStatus) {
    curStatus.setText(inCurStatus);
    curStatus.setVisible(true);
}

what is happening, is that the label, curStatus is not appearing. for example, here is a call:

gui1.setCurStatus("Now running diagnostics... Please wait!");
  • 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-28T22:55:45+00:00Added an answer on May 28, 2026 at 10:55 pm

    Your problem appears to be one of misplaced references.

    Here is how you create your GUI:

      public static void main(String[] args) {
         test gui = new test();
         gui.startGUI();
         // gui.setCurStatus("testing!"); << seems to work here,
         // but when i call it from another class, it doesn't want to run.
      }
    

    You create your “test” object (which should be named “Test” by the way to conform to Java naming conventions) inside of your main method. Since it is declared inside of main, this variable has scope only inside of main and is visible no where else.

    You then tell us that you are calling the method like so:

    gui1.setCurStatus("Now running diagnostics... Please wait!");
    

    The gui1 variable refers to a test class object but it likely refers to a different object than the test object that is being displayed since the original displayed test object is only refered to by a variable local to the main method.

    To solve this, you must make sure to call setCurStatus on the currently displayed test object. How to do this depends on the rest of your code, something you’ve refused to show us despite our requests for you to do so.

    Edit: Based on your latest bit of posted code (which still won’t compile for me since it is missing a method, createTasksFile(), my assumptions are correct, you are calling setCurStatus(...) on a gui object that is not the displayed one:

      public static String[] runDiagnostics() throws IOException {
    
         gui gui1 = new gui(); // (A)
         gui1.setCurStatus("Now running diagnostics... Please wait!");
    

    On line (A) you create a new gui object and call setCurStatus on it, but it is not the GUI object that is being displayed but a completely different and unrelated object. It’s only relation is that it is an object of the same class as the one being displayed but that’s it. The solution is to get a reference to the displayed GUI and call this method on that object, and that object only.

    Also, Robin’s assumptions are correct, in that even if you fix this, you’re going to be stuck with a Swing concurrency issue. The JLabel won’t update because the Swing thread is trying to open a file:

      public static String[] runDiagnostics() throws IOException {
    
         gui gui1 = new gui();
         gui1.setCurStatus("Now running diagnostics... Please wait!");
    
         int i = 0;
         int errorsI = 0;
         File f = new File("tasks.txt");
         String[] errors = { "", "", "", "", "" };
    
         // try to create the file three times
         do {
            f.createNewFile();
            i++;
         } while (!f.exists() && i < 3);
    

    So we’re both right. The solution to this is to open your file on a background thread, a SwingWorker would work nicely here.

    Edit 2
    So to fix the reference problem, pass a reference of the gui into the runDiagnostics method using a gui parameter. Then call the setCurStatus method on this parameter. For example:

      public static String[] runDiagnostics(gui gui1) throws IOException {
    
         //!! gui gui1 = new gui(); // !! no longer needed
         gui1.setCurStatus("Now running diagnostics... Please wait!");
    

    You would have to pass the GUI in when calling the method:

            //!! results = taskBckg.runDiagnostics();
            results = taskBckg.runDiagnostics(gui);
    

    Also, please edit all your code so that it follows Java naming conventions. All class names should begin with a capital letter. This makes it much easier for others to understand what your code is doing.

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

Sidebar

Related Questions

I'm having some trouble updating a row in a MySQL database. Here is the
I am having trouble updating my Database from my code using a DataSet. I'm
I'm having trouble with updating an ASP:UpdatePanel using javascript (jQuery). Here're what I have.
I am having some trouble updating UMDF drivers using devcon during a standard code-deploy-debug
I'm having trouble getting a JLabel to display text immediately before updating it with
I am having trouble updating components in primefaces. Here is one example: <h:form> <p:panel
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
I am having trouble updating a text area. I declare textArea in gui.java :
I am updating an application to Rails 3 and I am having trouble creating

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.