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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:43:03+00:00 2026-06-01T18:43:03+00:00

I have been trying to fix my .setText crash issue, which occures when i

  • 0

I have been trying to fix my .setText crash issue, which occures when i press the num0 button randomly.

  • But it seems i can only resolve it by using
    new Thread(new Runnable() { __overwrite__swing__textfields___ }); but in that case i can not access my JTextField.

Therefore i have written a RunnableOutput class to send and apply but t
hat also failing. Example:

RunnableOutput.java

package ui;

import java.awt.ComponentOrientation;
import javax.swing.*;

/**
 * JTextField - Wrapper for thread-safe 
 * 
 * @author root
 */
public class RunnableOutput implements Runnable {
  private JTextField outputArea;
  private String messageToAppend;

  // initialze outputArea and message
  public RunnableOutput(JTextField output, String message) {
    outputArea = output;
    messageToAppend = message;
  }

  @Override
  public void run() {
    outputArea.setText(messageToAppend);
  }

}

Menu.java:

public static JTextField nameTextField0 = new JTextField(20);
public void IPpanel(JPanel configPanel) { 

  JPanel Numlock = new JPanel(new GridLayout(0,12));
  JButton num0 = new JButton("0"); 
  num0.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent ae) {

      // Fails: 
      // was expecting this should fixed it but indeed still not
      SwingUtilities.invokeLater(
        new RunnableOutput(nameTextField0, "Unit test: 0"));       

      // Fails: 
      // wont work few times works but when randomly i press the 
      // button it fails
      // Same result with: SwingUtilities.invokeAndWait();
      SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          nameTextField0.setText("Unit test: 1");
        }
      });

      // Works: does not freez but does not change/apply the values
      new Thread(new Runnable() {
        Menu.nameTextField0.setText("Unit test: 2");
      });        

    }
  });
  Numlock.add(num0); 

  configPanel.setLayout(new GridLayout(0,1));
  configPanel.add(Numlock);

}

Follow up:

  • Used Polymorphism and that fix all those
  • 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-01T18:43:04+00:00Added an answer on June 1, 2026 at 6:43 pm

    1) you have to add code lines

    revalidate()
    repaint();
    

    in the case(s) you add JComponents to the already visible Container

    2) add/remove JComponents from/to the already visible Container must be done on EventDispatschTread, should be wrapped into invokeLater / invokeAndWait

    3) I’d suggest to wrapping (event are declared as Thread Safe methods) setText(), append() into invokeLater called from Runnable#Thread, Executor or util.Timer

    4) use SwingTimer (guarantted output on EDT) or Runnable#Thread (must be wrapped into invokeLater / invokeAndWait)

    5) for Swing GUI would be better to use SwingWorker, maybe better that Runnable#Thread with/or Executor/util.Timer

    6) don’t block EDT

    7) as adviced for better help sooner edit your question with a SSCCE

    EDIT

       new Thread(new Runnable() {
    
            public void run() {
    
                final RunnableOutput a = new RunnableOutput();
    
                if (SwingUtilities.isEventDispatchThread()) {
                    SwingUtilities.invokeLater(new Runnable() {
    
                        @Override
                        public void run() {
                            a.setText(nameTextField0, text);
                        }
                    });
                } else {
                    try {
                        SwingUtilities.invokeAndWait(new Runnable() {
    
                            @Override
                            public void run() {
                                a.setText(nameTextField0, text);
                            }
                        });
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InvocationTargetException ex) {
                        Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }).start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to fix this issue for weeks but I'm at the
I have a strange problem which I have been trying to fix for some
I have been trying desperately to fix this issue where my ListView items are
I have been trying to fix this for a while but I have had
So, I have been trying to fix this for about two months. It all
I really need some help with this as I have been trying to fix
I am trying to fix a regular expression i have been using in php
I have been trying this for a while but thus far haven't had any
Have been trying to fix this problem I have here. Basically I have a
I have been trying to fix this problem for hours. My app keeps crashing

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.