1

I am making a chat program in Java. When a user sends a message to the server, the server sends that message to every other user. The problem is, when a user is typing, he can receive a message, that will be written in the middle of the text he is typing.

A user must always be able to receive messages, while at the same time being able to write without having text appearing in the message he is typing.

Is there any way to do this in Java? A good solution would be to have a part of the console where the messages appear, and another one solely for typing.

It seems that there are libraries similar to ncurses, but are there any solution that doesn't rely on external libraries?

Cleaning the console using ANSI escape codes doesn't help since the text an user is typing might simply disappear because of it.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
user96649
  • 471
  • 1
  • 5
  • 22

1 Answers1

0

You can create 2 seperate console applications (one which reads, and one which only sends). That way your console windows would be separate and text would not conflict. Bear in mind that if you are using sessions, you would need to bind both sessions to the same user.

Another solution could be reading and storing each individual key without pressing enter. When you receive the response from the other client, you manually clear everything, print the text received, then place whatever the user was typing directly after.

Both of the above solutions seem hacky, and I feel Swing or JavaFX would be the way to go.

Gabriel Stellini
  • 426
  • 4
  • 13