Results 1 to 3 of 3
  1. #1
    mischaila is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default How to write in private chat room

    How to make a private chat room?
    I have the gui room but i need to select an user from the MyClient (common room) and start writing with him/her inside the private room.






    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    
    import javax.swing.DefaultListModel;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    
    
    
    
    
    public class MyClientPrivate2  extends JFrame implements ActionListener{
    		JFrame frame1;
    		JList list3;
    		JTextField tf1;
    		DefaultListModel model2;
    		JButton button;JButton backToOpen;JButton lout;
    		JScrollPane scrollpane2;
    		JLabel label;
    		Socket s,s1,s2,s3;
    		DataInputStream din2;
    		DataOutputStream dout3;
    		DataOutputStream dlout1;
    		DataOutputStream dout2;
    		DataInputStream din3;
    		String name1;
    		private String userName;
    		
    		private JMenuBar menuBar = new JMenuBar();
    		private JMenu file = new JMenu("File");
    		private JMenuItem logout = new JMenuItem("Logout");
    		private JMenuItem quit = new JMenuItem("Quit.");
    		
    		private JMenu status = new JMenu("Status");
    		private JMenuItem online = new JMenuItem("Online");
    		private JMenuItem away = new JMenuItem("Busy");
    		
    		private JMenu send = new JMenu("Send file");
    		private JMenuItem select = new JMenuItem("Select file");
    		
    		private JMenu help = new JMenu("Help");
    		private JMenuItem aboutUs = new JMenuItem("About us");
    		
    		
    		MyClientPrivate2(String name1)throws IOException{
    			frame1 = new JFrame("Private Chat");
    			tf1=new JTextField();
    	
    			model2=new DefaultListModel();
    			label=new JLabel("Message: ");
    			list3=new JList(model2);
    			button=new JButton("Send");
    			backToOpen = new JButton("Common Room");
    			lout=new JButton("LogOut");
    			scrollpane2=new JScrollPane(list3);
    			JPanel panel = new JPanel();
    			
    			//adding the menu here
    			this.setJMenuBar(menuBar);
    			panel.add(menuBar);
    			menuBar.add(file);
    			menuBar.add(status);
    			menuBar.add(send);
    			menuBar.add(help);
    			
    			file.add(logout);
    			file.add(quit);
    			status.add(online);
    			status.add(away);
    			send.add(select);
    			help.add(aboutUs);
    			
    			button.addActionListener(this);
    			backToOpen.addActionListener(this);
    			lout.addActionListener(this);
    			panel.add(tf1);
    			panel.add(scrollpane2);
    			panel.add(button);
    			panel.add(backToOpen);
    			panel.add(label);
    			panel.add(lout);
    		//	panel.add(menuBar);
    			
    			scrollpane2.setBounds(5,20,385,162);
    			label.setBounds(25,190,80,30);
    			tf1.setBounds(110,190,140,30);
    			button.setBounds(260,190,130,30);
    			backToOpen.setBounds(260,215,130,30);
    			lout.setBounds(260,240,130,30);
    			menuBar.setBounds(0, 0,500,20);
    			
    			
    			frame1.add(panel);
    			panel.setLayout(null);
    			frame1.setSize(410, 350);
    		   	frame1.setVisible(true);
    			frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			this.name1=name1;
    			//frame1.addWindowListener(this);
    			s=new Socket("localhost",1004);	//creates a socket object
    			s1=new Socket("localhost",1004);
    			s2=new Socket("localhost",1004);
    			s3 = new Socket("localhost",1004);
    		    //create inputstream for a particular socket
    			din2=new DataInputStream(s.getInputStream());
    			//create outputstream
    			dout3=new DataOutputStream(s.getOutputStream());
    			//sending a message for login
    			dout3.writeUTF(name1 +" has Logged in");	
    			dlout1=new DataOutputStream(s2.getOutputStream());
    			dout2=new DataOutputStream(s3.getOutputStream());
    			din3=new DataInputStream(s3.getInputStream());
    
    	// creating a thread for maintaning the list of user name
    			My2 m2=new My2(dout2,model2,name1,din2);
    			Thread t3=new Thread(m2);
    			t3.start();			
    		//creating a thread for receiving a messages
    			/*My m3=new My(din2,model2);
    		Thread t2=new Thread(m3);
    			t2.start();*/
    	  	}
    		
    		public void actionPerformed(ActionEvent e){
    			// sending the messages
    			if(e.getSource()==button){	
    				String str="";
    				str=tf1.getText();
    				tf1.setText("");
    				str="< "+ name1+ ": > "+str;
    				try{
    				dout3.writeUTF(str);
    				System.out.println(str);
    				dout3.flush();
    				}catch(IOException ae){System.out.println(ae);}
    			}
    			
    		if(e.getSource()==backToOpen){
    			try {
    				new MyClient(name1);
    			} catch (IOException e1) {
    				e1.printStackTrace();
    			}
    			
    				String str="";
    				str=backToOpen.getText();
    				tf1.setText("");
    				str="< "+name1+": >" +str;
    				try{
    				dout3.flush();
    		}catch(IOException ae){System.out.println(ae);} 
    		}
    		
    			// client logout
    			if (e.getSource()==lout){
    				frame1.dispose();
    				try{
    					 //sending the message for logout
    				dout3.writeUTF(name1 + userName+ " has Logged out");
    				dlout1.writeUTF(name1);
    				dlout1.flush();
    				Thread.currentThread();
    				Thread.sleep(1000);
    				System.exit(1);
    				}catch(Exception oe){}
    			}
    		}
    		
    		
    		public void windowClosing(WindowEvent w){
    			try{
    				dlout1.writeUTF(name1);
    				dlout1.flush();	
    				Thread.currentThread();
    				Thread.sleep(1000);
    				System.exit(1);
    				}catch(Exception oe){}
    			} 
    		//public static void main (String [] arg) throws IOException{
    			//new MyClientPrivate2("YUP");/
    		//}
    	}
    I also have this class:


    Java Code:
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.ObjectInputStream;
    import java.util.ArrayList;
    import java.util.Iterator;
    
    import javax.swing.DefaultListModel;
    
    public class My2 implements Runnable {
    	DataOutputStream dout2;
    	DefaultListModel model2;
    	DataInputStream din2;
    	String name1, lname1;
    	ArrayList alname1 = new ArrayList(); // stores the list of user names
    	ObjectInputStream obj1; // read the list of user names
    	int j = 0;
    
    	My2(DataOutputStream dout2, DefaultListModel model2, String name1,
    			DataInputStream din2) {
    		this.dout2 = dout2;
    		this.model2 = model2;
    		this.name1 = name1;
    		this.din2 = din2;
    	}
    
    	// class is used to received the messages
    	/*
    	 * class implements Runnable{ DataInputStream din1; DefaultListModel model1;
    	 * My(DataInputStream din, DefaultListModel model){ this.din1=din1;
    	 * this.model1=model1; } public void run(){ String str1=""; while(true){
    	 * try{ str1=din1.readUTF(); // receive the message // add the message in
    	 * list box model1.addElement(str1); }catch(Exception e){} } }
    	 */
    
    
    	public void run() {
    		try {
    			dout2.writeUTF(name1); // write the user name in output stream
    			while (true) {
    				obj1 = new ObjectInputStream(din2);
    				// read the list of user names
    				alname1 = (ArrayList) obj1.readObject();
    				if (j > 0)
    					model2.clear();
    				Iterator i1 = alname1.iterator();
    				System.out.println(alname1);
    				while (i1.hasNext()) {
    					lname1 = (String) i1.next();
    					j++;
    					// add the user names in list box
    					model2.addElement(lname1);
    				}
    			}
    		} catch (Exception oe) {
    		}
    	}
    	class My3 implements Runnable{
    		DataInputStream din2;
    		DefaultListModel model2;
    		My3(DataInputStream din2, DefaultListModel model2){
    			this.din2=din2;
    			this.model2=model2;
    		}
    			
    			public void run(){
    				String str1="";
    				while(true){
    					try{
    						str1=din2.readUTF(); // receive the message
    						// add the message in list box
    						model2.addElement(str1);
    					}catch(Exception e){}
    				}
    			}
    		}
    }
    Last edited by Eranga; 05-28-2010 at 02:03 PM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Please use code tags next time when you posting here in the forum. Unformated codes are really hard to read. If you don't know how to do, check on my signature.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. How to implement private chat application using jsp
    By PortalTeam in forum JavaServer Pages (JSP) and JSTL
    Replies: 9
    Last Post: 02-19-2010, 10:49 PM
  2. private
    By Gary_10 in forum New To Java
    Replies: 1
    Last Post: 01-29-2010, 12:42 AM
  3. Chat Room help
    By Noelf21 in forum New To Java
    Replies: 2
    Last Post: 01-13-2010, 01:28 PM
  4. Replies: 3
    Last Post: 04-20-2009, 09:53 PM
  5. Replies: 1
    Last Post: 08-09-2008, 11:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •