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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:07:26+00:00 2026-05-24T02:07:26+00:00

I have an issue setting a JLabel ‘s text using a method in the

  • 0

I have an issue setting a JLabel‘s text using a method in the class creating the GUI from a different class calling that method. The method to set the JLabel is called outside the GUI but when called from inside the GUI class it works. I have tested the getText() method on the label after it is called from outside the GUI class and it shows that the label has been updated. I get that it is probably a paint issue or update issue with Swing but I’m at a loss of what to do. I have tried repaint() and revalidate() on the label and then panel that it is within. Here is my current code:

public void setStatusLabel(String statusEntered) {
    //Shows the variable statusEntered has been received 
    System.out.println(statusEntered);

    //Not working
    status_label.setText(statusEntered);

    //Used this to check if the label receives the data. It does.
    String status = status_label.getText();
    System.out.println(status);
}

And the context in which I am calling it. Setups a database connection

//GUI Class reference
MainWindow mainwindow = new MainWindow();

public void connect(){
    Connection conn = null;
    try {
        String userName = "root";
        String password = "";
        String url = "jdbc:mysql://localhost:3306";
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        conn = DriverManager.getConnection(url, userName, password);

        //This works
        System.out.println("Connection Established");
        //The issue is with this guy
        mainwindow.setStatusLabel("Connection");
    }
    catch(Exception e) {
        System.err.println("Failed to connect to database");
        mainwindow.setStatusLabel("No connection");
    }
}

Any help with this would be awesome or if you have some links to suggestions, that would be awesome too! Thanks for the help.

Here is my main:

public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new MainWindow().setVisible(true);
        }
    });
}
  • 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-24T02:07:28+00:00Added an answer on May 24, 2026 at 2:07 am

    Your problem is possibly one of reference — your mainwindow variable in your GUI class may not be referring to the MainWindow object that is being displayed. Do you call new MainWindow(); anywhere else in your code? If so, then you’d best pass a reference to the visualized MainWindow into this class above so that you can call methods on it that will result in something that can be seen.

    For e.g.,

    public class DatabaseConnection {
       // MainWindow mainwindow = new MainWindow();  *** don't do this ***
       MainWindow mainwindow;
    
       public DatabaseConnection(MainWindow mainwindow) {
          this.mainwindow = mainwindow; // pass in the reference in the constructor
       }
    
       public void connect() {
          Connection conn = null;
    
          // ... etc
    
          // now we can call methods on the actual visualized object
          mainwindow.setStatusLabel("Connection");
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple issue setting a two-way databinding of a checkbox in Silverlight 3.0.
I have an issue setting up an external tool in Visual Studio. The tool
I have an issue with regards to using a ScaleTransform on a Canvas in
After setting up a replication on my server (SQL2005) I have noticed that default
I have an application that uses disabled JTextFields in several places which are intended
I'm having a bit of an issue setting up our test site. In IIS
Have another issue here. Our app starts out with a tab activity. But, before
I have an issue where the screen goes white for a millisecond on a
I have tried setting this up based on previously asked questionshere on stack overflow
Now that Maven3 has dropped support for setting uniqueVersion=false on deployment, I'm running into

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.