• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Client buttons remains focussed but its not receiving

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic