4

I am writing a socket programming. It has GUI for server and client. In the server GUI there is a textfield which shows the word requested by user. But I am having problem in showing the word.

I have tried

txtWord.setText(sentword);

It is not showing the word in the textfield. But when I write this

txtWord.setText(sentword);
JOptionPane.showMessageDialog(null, "the requesed word is: "+sentword);

then it shows the word in textfield and also shows it in the messagebox.

I have tried repaint() but it dint work. Please suggest me some solution as soon as possible

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Are you performing all operations on one thread? If so, you need to move all none-UI operations to different threads, otherwise you are blocking the UI thread, and you won't see UI updates. It seems like the case, as in your second example you do see the word, as the blocking operation is a UI operation, which not returning to your thread, but keeps updating the UI. – MByD Sep 23 '12 at 05:57

2 Answers2

9

as @Binyamin Sharet correctly commented, you have a Concurrency in Swing issue.

  • your Swing GUI doesn't care about long and hard tasks you're running in the background

  • even JTextField#setText() is declared as thread safe, output from Socket (i.e.) by default never notified Event Dispatch Thread

  • correct way could be to use a SwingWorker that has been created specifically to run long and hard tasks background to the Swing GUI and output to the GUI on event thread or EDT

  • or even easier is to use a Runnable in a Thread but making sure that all output to the Swing GUI is queued on the Swing event thread by placing it in a Runnable and calling it with invokeLater()

  • A dirty hack is to wrap code lines like so:

txtWord.setText(sentword);
JOptionPane.showMessageDialog(null, "the requesed word is: "+sentword);

into invokeLater(), but in this case your GUI will be unresponsive to Mouse or Keyboard events until Socket (in your case) ended

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    Sadly, [`setText()`](http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#setText%28java.lang.String%29) is no longer thread-safe in Java 7; see also [`Echo`](http://stackoverflow.com/a/3245805/230513). – trashgod Sep 23 '12 at 12:39
-1

txtWord.requestFocus(); textField does not show up until the window is over the textField and back or it gains focus, until Clicking on it. So... just request focus.

Also if check the text size if you had set while creation.Sometimes text not displayed if there is mismatch in size eg: txtWord.setSize(200, 24);

Alvin Pradeep
  • 618
  • 4
  • 13
  • 1
    -1 You wouldn't need a call to `setBounds` with a `LayoutManager`. And whether or not the textfield has the focus, calling `setText` should place the text inside the textfield – Robin Sep 23 '12 at 07:16
  • 1
    `setSize` is not needed either when using a `LayoutManager`. All sizing and positioning is done by the `LayoutManager` – Robin Sep 23 '12 at 07:58
  • Call `in/re/validate` on the parent container instead of `setBounds/Size` etc. – Andrew Thompson Sep 23 '12 at 09:29