Skip to content

Commit 03468ba

Browse files
authored
Add files via upload
1 parent 21c51e8 commit 03468ba

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

HelloController.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.example;
2+
3+
import javafx.application.Platform;
4+
import javafx.event.ActionEvent;
5+
import javafx.fxml.FXML;
6+
import javafx.scene.control.*;
7+
import javafx.scene.layout.AnchorPane;
8+
import javafx.stage.Stage;
9+
10+
import java.time.LocalTime;
11+
import java.time.format.DateTimeFormatter;
12+
13+
public class HelloController {
14+
15+
16+
@FXML
17+
private Button logoutButton;
18+
@FXML
19+
private AnchorPane scenePane;
20+
@FXML
21+
private ListView<String> chatList;
22+
@FXML
23+
private TextField messageField;
24+
@FXML
25+
private TextField usernameField;
26+
27+
private NtfyClient ntfy;
28+
private final DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("HH:mm");
29+
30+
31+
@FXML
32+
public void initialize() {
33+
// Set your topic name (you can make this configurable)
34+
String topic = "myfxchat";
35+
ntfy = new NtfyClient(topic);
36+
37+
// Start listening to the topic
38+
ntfy.subscribe(message -> Platform.runLater(() -> chatList.getItems().add(message)));
39+
}
40+
41+
@FXML
42+
private void handleSend() {
43+
String username = usernameField.getText().isBlank() ? "Anonymous" : usernameField.getText();
44+
String message = messageField.getText().trim();
45+
if (message.isEmpty()) return;
46+
47+
String time = LocalTime.now().format(timeFormat);
48+
String formatted = String.format("[%s] %s: %s", time, username, message);
49+
chatList.getItems().add(formatted);
50+
51+
ntfy.sendMessage(username, message);
52+
messageField.clear();
53+
}
54+
55+
Stage stage;
56+
public void logout(ActionEvent event) {
57+
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
58+
alert.setTitle("Log out");
59+
alert.setHeaderText("Log out");
60+
alert.setContentText("Are you sure you want to logout?");
61+
62+
if(alert.showAndWait().get() == ButtonType.OK){
63+
stage = (Stage) scenePane.getScene().getWindow();
64+
System.out.println("You have been logged out");
65+
stage.close();
66+
}
67+
}
68+
}
69+
70+
71+
72+
73+
74+

0 commit comments

Comments
 (0)