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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:52:36+00:00 2026-06-13T02:52:36+00:00

My program consists of a simple gui and a class that extends thread! i

  • 0

My program consists of a simple gui and a class that extends thread!

i am trying to learn how to use SwingUtilities.invokeLater() in order to use it to update a textfield in my GUI but how do i reach the textfield in my gui without making static? and am i on the right track or have i done something wrong so far:)?

Code

This has been taken from the Class called Client that extends thread this is where i want to update my GUI from using SwingUtilities.invokeLater(Runnable)

public void run(){
    while (socket.isConnected()) {

    if (input.hasNext()) {

        updateTextField();
    }

}
}

private void updateTextField() {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
  // here i want to update my textfield using txt.setText(input.nextLine());

        }
    });

}

UPDATE (This is my code so far) getting a nullpointer execption

public void run(){
    while (socket.isConnected()) {
            String x = input.next();
            System.out.println(x);
    mg.updateChat(x); // this is the line that gives me the nullexeption

    }
}

in my GUI

public void updateChat(final String input){
SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
    txtChat.setText(input); 
    }
}); 
}
  • 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-13T02:52:37+00:00Added an answer on June 13, 2026 at 2:52 am

    You can do it by having a final local variable in the method that contains the call to invokeLater(). You can access that variable from within the runnable object.

    For example:

    public void run(){
      while (socket.isConnected()) {
        if (input.hasNext()) {
          String nextInput = input.next();
          myGui.updateTextField(nextInput);
        }
      }
    }
    

    in your GUI class:

    public void updateTextField(final String nextInput) {
      SwingUtilities.invokeLater(
        new Runnable(){
          @Override
          public void run() {
            // assuming a private JTextField variable, myTextField
            myTextField.setText(nextInput);
          }
        }
      );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple program that consists of a .NET 2.0 exe (Program.exe)
Suppose I have a big program that consists of hundreds of methods in it.
I'm currently trying to optimize my program. I have a large database which consists
I'm writing a simple accounting program consists of several C# winform clients and a
The code below consists of two classes : SmartForm (simple model class) SmartForms (plural
I've got a simple program that uses an NSTimer to play a sound every
I am writing a simulation program that proceeds in discrete steps. The simulation consists
I am attempting to build a simple OpenGL 3.2 program which consists of a
I have a simple program written in C and Objective-C. It consists of a
I am trying to run my first Java program, an example script that interacts

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.