-2

I'm having a problem.. In my code I have it so that text should output to a JTextField. when I run the program, it doesn't. However, if I directy after my code for putting text into the JTextField put a JOptionPane then it works...

Anyone have an solution to make the JTextField update without having the JOptionPane after?

My code:

// Works:
JTextField.setText("String");
JOptionPane.showMessageDialog(null, "String");

// Doesn't Work:
JTextField.setText("String");
//JOptionPane.showMessageDialog(null, "String");
4
  • 1
    The example given is too vague to start with. Are you setting text in context of some thread that you started? Nov 2, 2011 at 15:56
  • You've got a bug in code that you're not showing us. I wonder if you may have a concurrency issue, as if you were trying to update the JTextField repeatedly perhaps in a loop with a call to Thread.sleep() and that's why it's not showing, but this is nothing but a SWAG til you tell us the details that we need to know. Nov 2, 2011 at 16:09
  • I'm not using any threads... But maybe the framework I have does... Nov 2, 2011 at 16:20
  • Only guessing is possible here. If you have problem formulate question correctly, you can find help here: sscce.org. Nov 3, 2011 at 16:56

2 Answers 2

2

There are two reasons why this might fail:

  1. You're calling setText() from outside the main (Swing) thread
  2. You're calling setText() from in the main (Swing) thread

In the first case, wrap the call in SwingUtilities.invokeLater().

In the latter case, you set the text but you're blocking the Swing thread, so the change can't be rendered. You will need to create a background worker to do the work and use SwingUtilities.invokeLater() to update the text field from your worker thread.

[EDIT] See the Swing tutorial for an example how to use background thread and how to update the UI from there: http://download.oracle.com/javase/tutorial/uiswing/concurrency/interim.html

5
  • 1
    Do you mean like this?? javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { JTextField.setText("Connecting you to #TeamZeale as " + n + ", hang on it might take a while..."); } }); Nov 2, 2011 at 16:47
  • 1
    It only works if you're doing all the work in a background thread. If you block the main thread, Swing will never be able to process events and no rendering will happen. The UI will also appear to be "frozen". Nov 2, 2011 at 16:50
  • 2
    @enjikaka: Please stop making us guess. Please show us more of your code and explain more of what you're doing as many of us have already requested of you. Nov 2, 2011 at 16:51
  • I am trying to make is so that when I run the function runBot() in njIRC.class it uses a function in Act.class named putInS(String n) to set the text of the JTextField named status in Main.java to the string I pass in. Nov 2, 2011 at 17:27
  • @enjikaka: This is irrelevant. In which thread is this code executed? Nov 3, 2011 at 8:08
0

Why not just use repaint() after the text update?

If memory serves me right that should address the issue of the text not appending.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.