Advertisement
Guest User

ClientChat.java

a guest
May 1st, 2013
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.net.*;
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import static java.lang.System.out;
  8.  
  9. public class ChatClient extends JFrame implements ActionListener {
  10.  
  11.     String uname;
  12.     PrintWriter pw;
  13.     BufferedReader br;
  14.     JTextArea taMessages, taUserList;
  15.     JTextField tfInput;
  16.     JButton btnSend, btnExit;
  17.     Socket client;
  18.  
  19.     public ChatClient(String uname, String servername) throws Exception {
  20.         super("Connected as: " + uname);  // set title for frame
  21.         this.uname = uname;
  22.         client = new Socket(servername, 18524);
  23.         br = new BufferedReader(new InputStreamReader(client.getInputStream()));
  24.         pw = new PrintWriter(client.getOutputStream(), true);
  25.         pw.println(uname);  // send name to server
  26.         //bring up the chat interface
  27.         buildInterface();
  28.         new MessagesThread().start();  // create thread for listening for messages
  29.     }
  30.  
  31.     public void buildInterface() {
  32.         btnSend = new JButton("Send");
  33.         btnExit = new JButton("Exit");
  34.         //chat area
  35.         taMessages = new JTextArea();
  36.         taMessages.setRows(10);
  37.         taMessages.setColumns(50);
  38.         taMessages.setEditable(false);
  39.         //online users list
  40.         taUserList = new JTextArea();
  41.         taUserList.setRows(10);
  42.         taUserList.setColumns(10);
  43.         taUserList.setEditable(false);
  44.         //top panel (chat area and online users list
  45.         JScrollPane chatPanel = new JScrollPane(taMessages, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  46.                 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  47.         JScrollPane onlineUsersPanel = new JScrollPane(taUserList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  48.                 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  49.         JPanel tp = new JPanel(new FlowLayout());
  50.         tp.add(chatPanel);
  51.         tp.add(onlineUsersPanel);
  52.         add(tp, "Center");
  53.         //user input field
  54.         tfInput = new JTextField(50);
  55.         //buttom panel (input field, send and exit)
  56.         JPanel bp = new JPanel(new FlowLayout());
  57.         bp.add(tfInput);
  58.         bp.add(btnSend);
  59.         bp.add(btnExit);
  60.         add(bp, "South");
  61.         btnSend.addActionListener(this);
  62.         tfInput.addActionListener(this);//allow user to press Enter key in order to send message
  63.         btnExit.addActionListener(this);
  64.         setSize(500, 300);
  65.         setVisible(true);
  66.         pack();
  67.     }
  68.  
  69.     @Override
  70.     public void actionPerformed(ActionEvent evt) {
  71.         if (evt.getSource() == btnExit) {
  72.             pw.println("end");  // send end to server so that server know about the termination
  73.             System.exit(0);
  74.         } else {
  75.             // send message to server
  76.             pw.println(tfInput.getText());
  77.         }
  78.     }
  79.  
  80.     public static void main(String args[]) {
  81.  
  82.         // take username from user
  83.         String name = JOptionPane.showInputDialog(null, "Enter your name: ", "Username",
  84.                 JOptionPane.PLAIN_MESSAGE);
  85.         String servername = "localhost";
  86.         try {
  87.             new ChatClient(name, servername);
  88.         } catch (Exception ex) {
  89.             out.println("Unable to connect to server.\nError: " + ex.getMessage());
  90.         }
  91.  
  92.     } // end of main
  93.  
  94.     // inner class for Messages Thread
  95.     class MessagesThread extends Thread {
  96.  
  97.         @Override
  98.         public void run() {
  99.             String line;
  100.             try {
  101.                 while (true) {
  102.                     line = br.readLine();
  103.                     taMessages.append(line + "\n");
  104.                     taMessages.setCaretPosition(taMessages.getDocument().getLength());//auto scroll to last message
  105.                 } // end of while
  106.             } catch (Exception ex) {
  107.             }
  108.         }
  109.     }
  110. } //  end of client
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement