Re: Newbie swing question



"Kd" <kandarp.jani@xxxxxxxxx> wrote in news:1133297401.586288.199490
@g47g2000cwa.googlegroups.com:

> 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.
>

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
> 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
>

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
> 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
>

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.

--
Beware the False Authority Syndrome
.



Relevant Pages

  • Re: What doesnt lend itself to OO?
    ... >> proxy and instructs the server to constuct the real object. ... rather than client code. ... If 'clock' is instantiated in the server, ... > for the server interface at the OOA level. ...
    (comp.object)
  • This is going straight to the pool room
    ... or not the client has privilege to do what they're trying to do, ... The server environment is this: ... 3GL User action Routines that Tier3 will execute on your behalf during the ... Routine Name: USER_INIT ...
    (comp.os.vms)
  • [Full-Disclosure] R: Full-Disclosure Digest, Vol 3, Issue 42
    ... Full-Disclosure Digest, Vol 3, Issue 42 ... SD Server 4.0.70 Directory Traversal Bug ... Arkeia Network Backup Client Remote Access ...
    (Full-Disclosure)
  • Re: What doesnt lend itself to OO?
    ... > rather than client code. ... no way to do that without also touching the object with clock semantics ... will not encapsulate both clock semantics and network semantics. ... The server can do whatever it wants ...
    (comp.object)
  • Re: porting curses driven to gui application
    ... > more than just replace the curses interface by a GUI. ... > don't have a X server (e.g. because they are just running Windows ... > running on the machine where the database is also running. ... > server and the client on the user side. ...
    (comp.unix.programmer)