For my Chat GUI, I'm trying to 'show' the messages styled as Facebook's Messenger does, or at least, in that manner. If I'm the one sending them, they should be aligned to the right, if I'm getting a message from anyone else, they should be aligned to the left, and their background-color should be different.
I've made something similar, but it really looks ugly, and on top of this, I cannot style them individually, but in the even-odd way, so it looks something like this, even tho, a message has never been sent to 'create' them.
The message structure is really simple
public void sendMessageOnClick(){
sendButton.setOnAction((e) -> {
String message = textInput.getText();
chatHistory.getItems().add("Sorin: " + message + "\n");
textInput.clear();
sendButton.setDisable(true);
});
}
The trick is to use a cell factory returing custom
ListCells:CSS Stylesheet:
BTW: For a alignment of the own user's message that does not overlap other usernames you need to set the with of
userConstraintsto the largest text width for the user names...