From 2579040ed7d10b7d1bc86bb2006197f84d5c380d Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 19:05:28 +0100 Subject: [PATCH 1/4] Add method to initialize timer --- src/main/java/ChatClient.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index ac9268b..b8fd458 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -1,6 +1,8 @@ import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.*; @@ -70,6 +72,7 @@ public class ChatClient extends JFrame implements KeyListener { private JTextArea outputTextArea; private JTextField inputTextField; private JScrollPane outputScrollPane; + private Timer timer; public ChatClient() { super("Chat"); @@ -80,6 +83,16 @@ public class ChatClient extends JFrame implements KeyListener { } } + private void initTimer() { + timer = new Timer(1000, new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + //Method to update time + } + }); + timer.start(); + } + private void initGui() { outputTextArea = new JTextArea(); outputTextArea.setEditable(false); @@ -107,6 +120,7 @@ public class ChatClient extends JFrame implements KeyListener { toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); initGui(); + initTimer(); while (true) { String message = fromServerReader.readLine(); From cc07a4f8deaaa14e7e5eb8d45d6aaaebd58287b1 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 19:12:40 +0100 Subject: [PATCH 2/4] Show current time in chat window --- src/main/java/ChatClient.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index b8fd458..d593022 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -7,6 +7,8 @@ import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.*; import java.net.Socket; +import java.text.SimpleDateFormat; +import java.util.Date; public class ChatClient extends JFrame implements KeyListener { public static class TR extends JFrame { @@ -73,6 +75,7 @@ public class ChatClient extends JFrame implements KeyListener { private JTextField inputTextField; private JScrollPane outputScrollPane; private Timer timer; + private JLabel timeLabel; public ChatClient() { super("Chat"); @@ -87,12 +90,18 @@ public class ChatClient extends JFrame implements KeyListener { timer = new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - //Method to update time + updateTime(); } }); timer.start(); } + private void updateTime() { + SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a"); + String currentTime = sdf.format(new Date()); + timeLabel.setText("Current Time: " + currentTime); + } + private void initGui() { outputTextArea = new JTextArea(); outputTextArea.setEditable(false); @@ -101,6 +110,10 @@ public class ChatClient extends JFrame implements KeyListener { outputScrollPane = new JScrollPane(outputTextArea); + timeLabel = new JLabel(); + updateTime(); + add(timeLabel, BorderLayout.NORTH); + inputTextField = new JTextField(); inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); inputTextField.addKeyListener(this); From 1df17949e0e61dbc255cee35160a9a08986e35dd Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 19:16:00 +0100 Subject: [PATCH 3/4] Change time format to 24 hr --- src/main/java/ChatClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index d593022..7ff74a6 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -97,7 +97,7 @@ public class ChatClient extends JFrame implements KeyListener { } private void updateTime() { - SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a"); + SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); String currentTime = sdf.format(new Date()); timeLabel.setText("Current Time: " + currentTime); } From 06e09a1a0b4e5dd5ca0b7f6185dcfe01686ed963 Mon Sep 17 00:00:00 2001 From: Alena Bandarovich Date: Fri, 9 Feb 2024 19:55:31 +0100 Subject: [PATCH 4/4] Bring window to input name in front --- src/main/java/ClientHandler.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index b30e37a..2371721 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -1,8 +1,7 @@ +import javax.swing.*; import java.io.*; import java.net.Socket; -import javax.swing.JOptionPane; - public class ClientHandler implements Runnable { private ChatServer chatServer; private Socket connectionToClient; @@ -15,7 +14,9 @@ public class ClientHandler implements Runnable { public ClientHandler(ChatServer chatServer, Socket connectionToClient) { this.chatServer = chatServer; this.connectionToClient = connectionToClient; - name = JOptionPane.showInputDialog("Benutzername für neuen Client vergeben"); + final JDialog dialog = new JDialog(); + dialog.setAlwaysOnTop(true); + name = JOptionPane.showInputDialog(dialog,"Benutzername für neuen Client vergeben"); new Thread(this).start();} // Start a new thread for this client handler