Chat System - Chat GUI

Introduction: This tutorial is the thirteenth in my Java Network Programming using KryoNet series, or eighth in creating a chat client and server system, in which we are going to be adding a chat GUI to the client. Previous: In the previous tutorial we created a login GUI for the client to allow for more interaction with the user. The System: We are going to give the user a GUI to interact with the system - to send messages and see the currently connected members of the chat. When a client connects, add them to a list. Send incoming messages to everyone within the client list except the sender - or send them one back saying that it is received, as confirmation. This Tutorial: In the previous tutorial we made a login GUI, this tutorial is going to be making the main chat GUI. Chat GUI: First we will make the chat GUI class. This is very similar to the login GUI but with a different layout...
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. import javax.swing.BorderFactory;
  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10. import javax.swing.JTextArea;
  11. import javax.swing.border.Border;
  12.  
  13. public class ChatGUI extends JFrame{
  14.        
  15.         JLabel label1 = new JLabel("Chat:");
  16.         static JTextArea area1 = new JTextArea("Welcome to the chat room!\n");
  17.         JTextArea area2 = new JTextArea();
  18.         JButton button1 = new JButton("Send Message");
  19.        
  20.         public ChatGUI() {
  21.                 Dimension dim = new Dimension(800, 600);
  22.                 setSize(dim);
  23.                 setPreferredSize(dim);
  24.                 setMinimumSize(dim);
  25.                 setMaximumSize(dim);
  26.                 setResizable(false);
  27.                 setLocationRelativeTo(null);
  28.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.                 setTitle("Client");
  30.                 setVisible(true);
  31.                 setLayout(null);
  32.                
  33.                 label1.setLayout(null);
  34.                 area1.setLayout(null);
  35.                 area2.setLayout(null);
  36.                 button1.setLayout(null);
  37.                
  38.                 label1.setSize(200, 40);
  39.                 label1.setLocation(10, 10);
  40.                 area1.setEditable(false);
  41.                 area1.setSize(700, 370);
  42.                 area1.setLocation(10, 40);
  43.                 area2.setEditable(true);
  44.                 area2.setSize(700, 60);
  45.                 area2.setLocation(10, 440);
  46.                 button1.setSize(200, 40);
  47.                 button1.setLocation(250, 520);
  48.                
  49.                 Border border = BorderFactory.createLineBorder(Color.BLACK);
  50.                 area1.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(10, 10, 10, 10)));          
  51.                 area2.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(10, 10, 10, 10)));          
  52.                
  53.                 add(label1);
  54.                 add(area1);
  55.                 add(area2);
  56.                 add(button1);
  57.         }
  58. }
Area1 is the main chat room which displays all the previously received messages, it is read only. Area2 is the message box where the user enters their own message followed by pressing the Send Message button, the area is editable (read only is false). Next we need to add a button listener like we did with the login GUI. This button will create a new Packet2Message and send it through the client...
  1. add(label1);
  2. add(area1);
  3. add(area2);
  4. add(button1);
  5.  
  6. button1.addActionListener(new ActionListener() {
  7.         public void actionPerformed(ActionEvent event) {
  8.                 Packet2Message packet = new Packet2Message();
  9.                 packet.message = area2.getText();
  10.                 client.sendTCP(packet);
  11.                 area2.setText("");
  12.         }
  13. });
First we need to make the client. This will be the same as the login GUI which also uses the ClientCustom's client. We create a global client in the ChatGUI class...
  1. Client client;
Then modify the constructor to accept a client argument, and set the global client to the parameter client...
  1. public ChatGUI(final Client cclient) {
  2.         client = cclient;
  3.         ...
  4. }
Running the ChatGUI: So finally we need to run the ChatGUI. Once the user has logged in we want to run the ChatGUI and get rid of the login GUI, so we are going to modify our connect script to return a true or false to see if the user has connected or not... -Was:-
  1. public static void connect(Client client, String loggedInUsername) {
  2.         Packet3UsernameReq req = new Packet3UsernameReq();
  3.         req.username = loggedInUsername;
  4.         client.sendTCP(req);
  5.         while (!userServerRes) {
  6.                 try {
  7.                         System.out.println("Sleeping...");
  8.                         Thread.sleep(500);
  9.                 }catch (Exception ex) {}
  10.         }
  11.         if (userServerAvailable) {
  12.                 System.out.println("That username is already taken. Please enter a new one... ");
  13.                 userServerRes = false; //Reset global booleans used for server username response
  14.         }else{ //false=available
  15.                 System.out.println("Connecting...");
  16.                 Packet1Connect con = new Packet1Connect();
  17.                 con.name = loggedInUsername;
  18.                 client.sendTCP(con);
  19.                 System.out.println("Connected as " + loggedInUsername);
  20.                 userServerRes = false; //Reset global booleans used for server username response
  21.         }
  22. }
-Now is:-
  1. public static boolean connect(Client client, String loggedInUsername) {
  2.         Packet3UsernameReq req = new Packet3UsernameReq();
  3.         req.username = loggedInUsername;
  4.         client.sendTCP(req);
  5.         while (!userServerRes) {
  6.                 try {
  7.                         System.out.println("Sleeping...");
  8.                         Thread.sleep(500);
  9.                 }catch (Exception ex) {}
  10.         }
  11.         if (userServerAvailable) {
  12.                 System.out.println("That username is already taken. Please enter a new one... ");
  13.                 userServerRes = false; //Reset global booleans used for server username response
  14.                 return false;
  15.         }else{ //false=available
  16.                 System.out.println("Connecting...");
  17.                 Packet1Connect con = new Packet1Connect();
  18.                 con.name = loggedInUsername;
  19.                 client.sendTCP(con);
  20.                 System.out.println("Connected as " + loggedInUsername);
  21.                 userServerRes = false; //Reset global booleans used for server username response
  22.                 return true;
  23.         }
  24. }
Then modify the login GUI button action to run the new ChatGUI if the user logs in successfully. It will give the new ChatGUI the same client it gets given from the main class...
  1. button1.addActionListener(new ActionListener() {
  2.         public void actionPerformed(ActionEvent event) {
  3.                 if (ClientCustom.connect(client, text1.getText())) {
  4.                         dispose();
  5.                         new ChatGUI(client);
  6.                 }
  7.         }
  8. });
This code will now try to login, if it does login successfully (the username is not already taken and the server can be reached and vice versa - the client receives data from the server) it disposes the loginGUI (closes/hides it) and create (opens/shows) a new ChatGUI window. Next Tutorial: Since we have covered a lot in this tutorial, the next tutorial will be making the ChatGUI actions work properly (update received messages and add a users list).

Comments

Add new comment