Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Client buttons remains focussed but its not receiving  RSS feed

 
Zulfi Khan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to do communication between Client & server using GUI environment using NetBeans (socket programming). In my server I have 2 text fields and one button. I would write string data in text field and send to client by pressing button. My complete server code is given below.My  button handler name is:
private void btnSentToClientActionPerformed :



I have attached the server GUI serverGUI.jpeg & my client GUI is Client GUI.jpg.  My client code is:




I can send data from Server to Client. Client comes in the handler & prints “Enter1” but its printing Enter2. So its not receiving & client  “display” button remains focused.



Some body please guide me why client is blocking at ds.receive()?

Zulfi.


Server-GUI.jpg
[Thumbnail for Server-GUI.jpg]
Server Program GUI
Client-GUI.jpg
[Thumbnail for Client-GUI.jpg]
Client Program GUI
 
Rob Spoor
Sheriff
Posts: 21088
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your call to ds.receive(p) is blocking, which causes your entire Event Dispatcher Thread to be blocked. It can't do anything anymore until a datagram is received; that includes updating the UI. Read Concurrency in Swing for more information.

Solution: use a background thread to do the listening, then use EventQueue.invokeLater (or its alias SwingUtilities.invokeLater) to do any updates to the UI.
 
Zulfi Khan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for your response. Are you talking about the following stuff:

"

It's useful to think of the code running on the event dispatch thread as a series of short tasks. Most tasks are invocations of event-handling methods, such as ActionListener.actionPerformed. Other tasks can be scheduled by application code, using invokeLater or invokeAndWait. Tasks on the event dispatch thread must finish quickly; if they don't, unhandled events back up and the user interface becomes unresponsive.


"JavaDocs

I am trying to understand your hint


use a background thread to do the listening, then use EventQueue.invokeLater (or its alias SwingUtilities.invokeLater) to do any updates to the UI.


You mean i have to create a thread which would do the receive operation. After receiving the data it should call  EventQueue.invokeLater. am i right?

Zulfi.
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of creating your own Thread and using SwingUtilities.invokeLater() you can use a SwingWorker. It creates the Thread for you and allows you to "publish" data as it becomes available. A SwingWorker example is given in the Concurrency tutorial.
 
Zulfi Khan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have created the worker thread in the client program. But i dont know how it can end up unresponsiveness. My code is below: I have also added the button handler but i cant understand how the worker thread would take control jButton1ActionPerformed Method.



Some body please guide me.

Zulfi.
 
Dave Tolls
Rancher
Posts: 2912
35
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is your SwingWorker called?

I would expect to see it created and launched in the actionPerformed method for the button.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!