-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientWindow.java
More file actions
91 lines (76 loc) · 2.52 KB
/
ClientWindow.java
File metadata and controls
91 lines (76 loc) · 2.52 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
package chat.client;
import chat.network.TCPConnection;
import chat.network.TCPConnectionListener;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javax.swing.*;
import java.io.IOException;
public class ClientWindow extends Application implements TCPConnectionListener {
private static final String IP_ADDRES = "192.168.1.190";
private static final int PORT = 8189;
@FXML
private TextArea textArea;
@FXML
private TextField writeArea;
@FXML
private TextField nameArea;
@FXML
private Button send;
TCPConnection connection;
public void buttonClickHandler(ActionEvent evt){
String msg = writeArea.getText();
if(msg.equals("") || msg.equals(null)) return;
writeArea.setText(null);
connection.sendString(nameArea.getText() + ": " + msg);
}
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Java Chat");
primaryStage.setScene(new Scene(root, 590, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
new ClientWindow();
}
});
}
public ClientWindow(){
try {
connection = new TCPConnection(this, IP_ADDRES, PORT);
} catch (IOException e) {
printMsg("Connection Exception" + e);
}
}
@Override
public void onConnectionReady(TCPConnection tcpConnection) {
printMsg("Connection ready...");
}
@Override
public void onReceiveString(TCPConnection tcpConnection, String value) {
printMsg(value);
}
@Override
public void onDisconnect(TCPConnection tcpConnection) {
printMsg("Connection close...");
}
@Override
public void onException(TCPConnection tcpConnection, Exception e) {
printMsg("Connection Exception" + e);
}
private synchronized void printMsg(String msg){
SwingUtilities.invokeLater(() -> textArea.appendText(msg + "\n"));
}
}