auron344
auron344

Reputation: 81

How to update Swing GUI from other instances of program

I am doing a board strategy game. The thing is that I did a previous room chat where connected players can choose his color with a comboBox (similar to Age of Empires 2) and talk until host starts the game.

The thing is that i dont know how to update the GUI of the other players to show that the player X has picked color red.

I already connected the creator of the game (host) with the client, and i can send messages by console to each other. But I really dont know how to update both GUI after a change from a simple comboBox.

If Player 2 sets his color to green

then it must show green in the other player GUIs but i dont know how to do it.

Upvotes: 1

Views: 117

Answers (3)

Adam Dyga
Adam Dyga

Reputation: 8896

You have to add a listener to combobox. The listener should send a special message (command) to the host together with the color (something like "playerColorChanged #00ff00") and the host has to distribute it to all other clients (additionally appending source player name/ID). The clients have to update their GUI accordingly then.

I don't know your code, but probably the current communication protocol between the client and host will have to be updated in order to send such "system messages".

Upvotes: 1

Alan Krueger
Alan Krueger

Reputation: 4786

Depending on how your client/server protocol is written, you should have some way of sending different message types. Have each client send a specific message type to the server announcing when a player chooses a color which is then forwarded to all the other clients. Each of those clients, upon receiving that message, changes the relevant UI indicator.

(The approach can be similar with a peer-to-peer protocol.)

Upvotes: 0

Mythul
Mythul

Reputation: 1807

You could try implementing the Observer design pattern.

Upvotes: 1

Related Questions