Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Newbie swing question

Thread view: 
Kd - 29 Nov 2005 20:50 GMT
Hello,

I am new to java and I started off by writing a client/server based
tic-tac-toe game using Swing. I have run myself into the following
situation.

What I am trying to do:
My server wants print a dot (.) every few seconds on its GUI text area
when it is waiting for input from the client.

How I did it:
I have timeout set on the socket from where server is reading for the
client input. Upon timeout there is an exception and my catch block
will print(try to) a dot on the server GUI text area. And then it will
again go back to reading(waiting for) the client input.

Problem:
However out of my design flaw or whatever, this whole thing about
waiting for Client input and printing the GUI, is happening in the same
GUI thread.

So the User Inputs -> Server GUIThread ->Waits for Client->Timesout and
prints to the GUI->Wait for Client->Gets Client response->Prints some
other info to the GUI

Due to this the dot(.) that i m trying to print at time out is not
flushing on the GUI screen until  the server gets the response from the
client.

Is there a way to go around this ? If no what would be a better design
for Client/Server GUI apps WRT java swing.

Thanks,
Kandarp
zero - 29 Nov 2005 21:01 GMT
"Kd" <kandarp.jani@gmail.com> wrote in news:1133297401.586288.199490
@g47g2000cwa.googlegroups.com:

> Hello,
>
[quoted text clipped - 11 lines]
> will print(try to) a dot on the server GUI text area. And then it will
> again go back to reading(waiting for) the client input.

Bad idea.  Don't use exceptions to influence program flow.  Exceptions
are supposed to be exceptional, not the rule.

> Problem:
> However out of my design flaw or whatever, this whole thing about
[quoted text clipped - 4 lines]
> prints to the GUI->Wait for Client->Gets Client response->Prints some
> other info to the GUI

Have you looked into SwingUtilities.invokeLater?

> Due to this the dot(.) that i m trying to print at time out is not
> flushing on the GUI screen until  the server gets the response from the
[quoted text clipped - 5 lines]
> Thanks,
> Kandarp

a possible algorithm:

1a. server makes a move
1b. server starts DotThread

2a. server receives move from client
2b. server stops DotThread

DotThread would use Thread.sleep() to wait a number of seconds, and then
call

SwingUtilities.invokeLater(new Runnable()
  {
     public void run()
     {
        textOutput.append(".");
     }
  });

An alternative is to use a java.util.Timer which wakes up DotThread
(which would then extend TimerTask) at certain intervals.  In this
implementation DotThread would not use Thread.sleep of course.

Signature

Beware the False Authority Syndrome

Kd - 30 Nov 2005 19:33 GMT
I see. This makes sense. I am familiar with invokeLater. Because when I
googled around invokeLater is what I was getting as a solution. I tired
using it but it didnot work.

The problem is that I was trying to take the user input (server move)
and then process the whole thing serially. I guess should take the
(userinput) move made on the server side in one thread. Then other
threads should handle the processing (which involves the DotThread and
the Client thread). That way the event handling thread of Swing is not
blocked waiting for response from the client. Which is what was
happening with my design

Thanks for you reply
Kandarp


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.