-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientGUI.java
More file actions
171 lines (139 loc) · 5.9 KB
/
ClientGUI.java
File metadata and controls
171 lines (139 loc) · 5.9 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.io.*;
import java.net.Socket;
import java.util.Arrays;
import java.util.List;
public class ClientGUI extends Application {
private PrintWriter writer;
private BufferedReader reader;
private VBox messageContainer;
private ScrollPane scrollPane;
// NEW: The list of active users
private ListView<String> userList;
private ObservableList<String> usersObservable;
private String username;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
TextInputDialog dialog = new TextInputDialog("");
dialog.setTitle("Login");
dialog.setHeaderText("Welcome to ChatApp");
dialog.setContentText("Username:");
dialog.getDialogPane().lookupButton(ButtonType.CANCEL).setVisible(false);
var result = dialog.showAndWait();
if (result.isPresent() && !result.get().trim().isEmpty()) {
username = result.get().trim();
} else {
return;
}
primaryStage.setTitle("Chat - " + username);
// --- 1. LEFT SIDE: USER LIST ---
usersObservable = FXCollections.observableArrayList();
userList = new ListView<>(usersObservable);
userList.setPrefWidth(120);
userList.setStyle("-fx-background-color: #f0f2f5; -fx-border-color: #ddd;");
VBox leftBox = new VBox(new Label("Online Users"), userList);
leftBox.setPadding(new Insets(10));
leftBox.setStyle("-fx-background-color: #ffffff;");
// --- 2. CENTER: CHAT AREA ---
messageContainer = new VBox(10);
messageContainer.setPadding(new Insets(10));
scrollPane = new ScrollPane(messageContainer);
scrollPane.setFitToWidth(true);
scrollPane.setVvalue(1.0);
// --- 3. BOTTOM: INPUT ---
TextField inputField = new TextField();
inputField.setPromptText("Type a message...");
HBox.setHgrow(inputField, Priority.ALWAYS);
Button btnSend = new Button("➤");
HBox inputBox = new HBox(10, inputField, btnSend);
inputBox.setAlignment(Pos.CENTER);
inputBox.getStyleClass().add("input-box");
btnSend.setOnAction(e -> sendMessage(inputField));
inputField.setOnAction(e -> sendMessage(inputField));
// --- LAYOUT ---
BorderPane root = new BorderPane();
root.setLeft(leftBox); // Add the list to the left
root.setCenter(scrollPane);
root.setBottom(inputBox);
Scene scene = new Scene(root, 550, 500); // Made it slightly wider
try { scene.getStylesheets().add(getClass().getResource("chat.css").toExternalForm()); }
catch (Exception e) {/*ignore*/}
primaryStage.setScene(scene);
primaryStage.show();
connectToServer();
}
private void connectToServer() {
new Thread(() -> {
try {
Socket socket = new Socket("localhost", 1234);
writer = new PrintWriter(socket.getOutputStream(), true);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer.println(username);
String msg;
while ((msg = reader.readLine()) != null) {
String finalMsg = msg;
Platform.runLater(() -> processIncomingMessage(finalMsg));
}
} catch (IOException e) {
Platform.runLater(() -> addSystemMessage("Error: Server not found."));
}
}).start();
}
// --- NEW LOGIC: Distinguish Chat vs User List ---
private void processIncomingMessage(String msg) {
if (msg.startsWith("USERS:")) {
// It's a list update! "USERS:Alice,Bob,Charlie,"
String cleanMsg = msg.replace("USERS:", "");
String[] names = cleanMsg.split(",");
// Update the sidebar list
usersObservable.clear();
usersObservable.addAll(Arrays.asList(names));
} else if (msg.startsWith("SERVER:")) {
addSystemMessage(msg.replace("SERVER: ", ""));
} else if (msg.contains(": ")) {
String[] parts = msg.split(": ", 2);
if (!parts[0].equals(username)) {
addBubble(parts[0] + "\n" + parts[1], false);
}
}
}
private void sendMessage(TextField input) {
String msg = input.getText().trim();
if (!msg.isEmpty() && writer != null) {
writer.println(username + ": " + msg);
addBubble(msg, true);
input.clear();
}
}
private void addBubble(String text, boolean isMyMessage) {
Label bubble = new Label(text);
bubble.setWrapText(true);
bubble.setMaxWidth(250);
bubble.getStyleClass().add("chat-bubble");
bubble.getStyleClass().add(isMyMessage ? "my-message" : "other-message");
HBox container = new HBox(bubble);
container.setAlignment(isMyMessage ? Pos.CENTER_RIGHT : Pos.CENTER_LEFT);
messageContainer.getChildren().add(container);
scrollPane.layout();
scrollPane.setVvalue(1.0);
}
private void addSystemMessage(String text) {
Label label = new Label(text);
label.setStyle("-fx-font-size: 10px; -fx-text-fill: #555; -fx-padding: 5px;");
HBox container = new HBox(label);
container.setAlignment(Pos.CENTER);
messageContainer.getChildren().add(container);
}
}