I
I
ideas4ru2012-02-17 07:32:44
Java
ideas4ru, 2012-02-17 07:32:44

Java: GUI Interaction with Network Module

Good afternoon,

To study Java, I decided to write a network game with a client and server part. The client roughly speaking consists of two classes: Connection, UserInterface. While the UserInterface was purely console everything was great.

command = userInterface.getCommand(); // blocking call
sendToServer(command);

where inside getCommand is System.in.read();

Now I'm changing from a console interface to a Swing-based GUI and want to call
command = userInterface.getCommand(); like so.

but the situation is different now. getCommand() should only return when the user fires an event with the mouse or keyboard.

In this regard, it is not very clear to me how the getCommand function can be implemented.

Any suggestions are welcome, a link to something to read.

Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dzigoro, 2012-02-17
@dzigoro

Everything is fine, only getCommand must be called on the SwingWorker. Read here . The point is to perform all at least some long operations in a separate thread without blocking the UI.

B
BrightGenie, 2012-02-17
@BrightGenie

add a MouseListener and a KeyListener to your swing object. and as a result of the desired keyboard shortcuts or on mouseReleased call sendToServer(command)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question