-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatWindow.java
More file actions
75 lines (63 loc) · 2.1 KB
/
ChatWindow.java
File metadata and controls
75 lines (63 loc) · 2.1 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
/*
this should display a user's conversations
is the controller and view in mvc
sends to the server
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ChatWindow {
Server host;
String windowName;
User user; // the chat is from this user's perspective
Chat chat;
JTextArea chatArea = new JTextArea();
ChatWindow(String wn, Chat c, Server h, User u) {
host = h;
windowName = wn;
chat = c;
user = u;
h.wm.chatWindowSet.add(this);
JPanel chatAreaPanel = new JPanel(new BorderLayout());
JPanel messageAndSendPanel = new JPanel(new BorderLayout());
Button sendButton = new Button("Send");
chatArea.setEditable(false);
JTextField messageField = new JTextField();
JScrollPane scrollingChatArea = new JScrollPane(chatArea);
JFrame frame = new JFrame(windowName);
frame.setLayout(new BorderLayout());
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
h.sendMessage(user, messageField.getText(), chat);
messageField.setText("");
updateChatFeed();
}
});
updateChatFeed();
chatAreaPanel.add(scrollingChatArea, BorderLayout.CENTER);
frame.add(chatAreaPanel, BorderLayout.CENTER);
messageAndSendPanel.add(messageField, BorderLayout.CENTER);
messageAndSendPanel.add(sendButton, BorderLayout.EAST);
frame.add(messageAndSendPanel, BorderLayout.SOUTH);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
h.wm.chatWindowSet.remove(this);
super.windowClosing(e);
}
});
frame.setSize(500,600);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void updateChatFeed() {
chatArea.setText(chat.generateMessageFeed());
chatArea.revalidate();
chatArea.repaint();
}
}