View the step-by-step solution to:

Hi there, I have to make a chat frame with all these requirements in it. can anyone help me with this?

Hi there, I have to make a chat frame with all these requirements in it. can anyone help me with this?

Set name of user (username):							
	- anything typed into textfield before connecting will be username
	- add username to title bar of frame
	- prepend name to all messages
	- announce entrances and exits
User should be able to connect and disconnect:					
	- user list of all members should update correctly

Private Messages:								
	 - if a name is selected on the user list, the message will only be sent
		to that user

The code provided is for ChatServer is:

import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer{  
	public static void main(String[] args ){  
		ArrayList<ChatHandler>handlers = new ArrayList<ChatHandler>();
		try{  
			ServerSocket s = new ServerSocket(3000);
			for(;;){
				Socket incoming = s.accept();
				new ChatHandler(incoming, handlers).start();
			}
		}catch (Exception e){  
			System.out.println(e);
		} 
	} 
}	  
	  
class ChatHandler extends Thread{
	DataObject myObject = null;
	private Socket incoming;
	ArrayList<ChatHandler>handlers;
	ObjectInputStream in;
	ObjectOutputStream out;
	
	public ChatHandler(Socket i, ArrayList<ChatHandler>h){
		incoming = i;
		handlers = h;
		handlers.add(this);
	}
	public void run(){
		try{
			in = new ObjectInputStream(incoming.getInputStream());
			out = new ObjectOutputStream(incoming.getOutputStream());

			boolean done = false;
			while (!done){  
				DataObject objIn = (DataObject)in.readObject();
				if (objIn == null){
					done = true;
				}else{
					for(ChatHandler h : handlers){
						h.out.writeObject(objIn);
					}
					if (objIn.getMessage().trim().equals("BYE")){ 
						done = true;
					}
				}
			}
			incoming.close();
		}catch (Exception e){  
			System.out.println(e);
		}finally{
			handlers.remove(this);
		} 
	} 
}

The code for ChatFrame is:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;

public class ChatFrame extends Frame{
	public ChatFrame(){
		setSize(500, 500);
		setTitle("Chat Frame");
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we){
				System.exit(0);
			}
		});
		add(new ChatPanel(), BorderLayout.CENTER);
		setVisible(true);
	}
	public static void main(String[] args){
		new ChatFrame();
	}
}

class ChatPanel extends Panel implements ActionListener, Runnable{  //INCOMPLETE!!!
	TextArea ta;
	TextField tf;
	Button connect, disconnect;
	Thread thread;
	Socket s;
	ObjectInputStream ois;
	ObjectOutputStream oos;
	DataObject d1,d2;
	String username;
	boolean connected;
	
	public ChatPanel(){
			setLayout(new BorderLayout());
			tf = new TextField();
			tf.addActionListener(this);
			ta = new TextArea();
			add(tf, BorderLayout.NORTH);
			Panel p1 = new Panel();
			p1.setLayout(new BorderLayout());
			p1.add(ta, BorderLayout.CENTER);
			java.awt.List list = new java.awt.List();
			p1.add(list, BorderLayout.WEST);
			add(p1, BorderLayout.CENTER);
			connect = new Button("Connect");
			connect.addActionListener(this);
			disconnect = new Button("Disconnect");
			disconnect.setEnabled(false);
			disconnect.addActionListener(this);
			Panel p2 = new Panel();
			p2.add(connect);
			p2.add(disconnect);
			add(p2, BorderLayout.SOUTH);
		
			
			
		
		
	}
	public void actionPerformed(ActionEvent ae){
		if(ae.getSource() == connect){
			if(!connected){
				try{
					s = new Socket("127.0.0.1", 3000);
					oos = new ObjectOutputStream(s.getOutputStream());
					ois = new ObjectInputStream(s.getInputStream());
					thread = new Thread(this);
					thread.start();
					connected = true;
					connect.setEnabled(false);
					System.out.println("Connected!!!");
				}catch(UnknownHostException uhe){
					System.out.println(uhe.getMessage());
				}catch(IOException ioe){
					System.out.println(ioe.getMessage());
				}
			}
		}else if(ae.getSource() == disconnect){
			if(connected){
				

			}
		}else if(ae.getSource() == tf){
			if(connected){
				try{
					String temp = tf.getText();
					d1 = new DataObject();
					d1.setMessage(temp);
					oos.writeObject(d1);
					tf.setText("");
				}catch(IOException ioe){
					System.out.println(ioe.getMessage());
				}		
			}
		}
	}
	public void run(){
		while(connected){
			try{
				d2 = (DataObject)ois.readObject();
				String temp = d2.getMessage();
				ta.append(temp + "n");	

			}catch(IOException ioe){
				System.out.println(ioe.getMessage());
			}catch(ClassNotFoundException cnfe){
				System.out.println(cnfe.getMessage());
			}
		}
	}
}

The code for data object is :

import java.io.*;
import java.util.*;

public class DataObject implements Serializable{

	private String message;

	DataObject(){
		message = "";
	}

	public String getMessage(){
		return message;
	}

	public void setMessage(String inMessage){
		message = inMessage;
	}
}

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question