-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientChatroomController.java
86 lines (69 loc) · 2.05 KB
/
ClientChatroomController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package assignment7;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
public class ClientChatroomController implements Initializable {
@FXML
private ListView<String> chatText;
@FXML
private ListView<String> chatClients;
@FXML
private TextField messageField;
private ChatClient client;
private String chatName;
public boolean isPrivate;
public ClientChatroomController() {
}
public void setName(String chatName) {
System.out.println(chatName);
this.chatName = chatName;
}
public void setClient(ChatClient client) {
this.client = client;
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
messageField.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getCode() == KeyCode.ENTER) {
client.sendMessage(messageField.getText(), chatName);
messageField.clear();
}
}
});
}
public void updateChat(String message) {
Platform.runLater(new Runnable() {
@Override
public void run() {
ObservableList<String> messages = FXCollections.observableArrayList(Arrays.asList(message));
ObservableList<String> history = chatText.getItems();
messages.addAll(history);
chatText.setItems(messages);
}
});
}
public void updateClientList(ObservableList<String> list) {
Platform.runLater(new Runnable() {
@Override
public void run() {
chatClients.getItems().clear();
chatClients.setItems(list);
}
});
}
}