Page 1 of 3 123 LastLast
Results 1 to 20 of 60
Like Tree2Likes

Thread: How update Jlist in java Chat

  1. #1
    pappyj77 is offline Member
    Join Date
    Jul 2011
    Location
    kent, UK
    Posts
    47
    Rep Power
    0

    Default How update Jlist in java Chat

    Hi everyone, am sorry, i have a problem here, believe me i have tried as much as i can. but unfortunately couldn't get the back of it, before thinking of posting for help. and thank you in advance for ur time and effort.

    i am trying to update my Client JList (userList) so that when a new client comes in, the initial client would be updated. but at present, when there's a single client, it can see itself but when another client enters the chat, only that client sees the two clients connected, the initial client is not updated with the name on the JList.
    please help simplify any answer because am not expert in java. thank you!!

    the Client code
    Java Code:
    import java.io.*;
         import java.net.*;
         import java.awt.*;
         import java.awt.event.*;
         import javax.swing.*;
    
          public class  ClientChat extends JFrame {
    	private Toolkit toolkit; 
    	private JLabel msgLabel;
    	private JButton sendBtn;
    	private JTextArea genMsg;
    	public static JList userList;
    	private JTextField msgF;
    	private ObjectOutputStream output;
    	private ObjectInputStream input;
    	private Socket client;
    	private String chatServer;
    	private int serverport;
    	private String Client_name;
    	
    	public ClientChat( String host, int port,String C_Name){
    
    		// set server to which this client connects
    		chatServer = host;
    		serverport = port;
    		Client_name = C_Name;
    
    
    		toolkit = Toolkit.getDefaultToolkit();		
    		if(toolkit.getScreenSize().getWidth() > 600)
    			setSize(600, 605);
    		else
    			setSize((int)toolkit.getScreenSize().getWidth(),(int)toolkit.getScreenSize().getHeight() - 20);			
    			setResizable(false);
    			Dimension dimension = getSize();	
    			setLayout(new FlowLayout());
    
    			setTitle("FRESHER MARKETING COMPANY");		
    			addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0);}});
    
    			Container container = getContentPane();
    			container.setLayout(new FlowLayout());
    
    			// create General Message Screen
    			genMsg = new JTextArea(30,43);
    			genMsg.setEditable(false);
    			genMsg.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
    			container.add( new JScrollPane( genMsg ));
    			JPanel genMsgPanel = new JPanel();
    			genMsgPanel.setLayout(new BorderLayout());
    			genMsgPanel.add(new JScrollPane(genMsg), BorderLayout.EAST);
    			genMsgPanel.setBorder(BorderFactory.createLineBorder(Color.black));	
    			container.add(genMsgPanel);
    
    			// create Friend List View
    			userList = new JList();
    			userList.setPreferredSize(new Dimension(150,423));
    			userList.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
    			container.add(userList);
    			JPanel userListPanel = new JPanel();
    			userListPanel.setLayout(new BorderLayout());
    			userListPanel.add(userList, BorderLayout.CENTER);
    			userListPanel.setBorder(BorderFactory.createLineBorder(Color.black));	
    			container.add(userListPanel);
    
    			msgLabel = new JLabel ("Message:");
    			container.add(msgLabel);
    			JPanel msgFPanel = new JPanel();
    			msgFPanel.setLayout(new BorderLayout());
    			msgFPanel.add(new JScrollPane(msgLabel), BorderLayout.WEST);
    			container.add(msgFPanel);
    
    			// create Message Field
    			msgF = new JTextField(37);
    			msgF.setEnabled( true );
    			msgF.setText("");
    			msgF.requestFocus();
    			msgF.addActionListener(
    				new ActionListener() {
    
    				// send message to server
    					public void actionPerformed( ActionEvent event )
    					{
    						//sendData( event.getActionCommand() );
    					}
    				} // end anonymous inner class
    				); // end call to addActionListener
    			msgFPanel.add(new JScrollPane(msgF), BorderLayout.CENTER);
    				}
    	public void runClient()
    	{
    		// connect to server, get streams, process connection
    		try {
    			// Step 1: Create a Socket to make connection
    			connectToServer();
    			// Step 2: Get the input and output streams
    			getStreams();
    			handShake();
    		}// server closed connection
    		catch ( EOFException eofException ) {
    			System.out.println( "Server terminated connection" );
    		}
    
    		// process problems communicating with server
    		catch ( IOException ioException ) {
    			ioException.printStackTrace();
    		}
    	}
    	private void connectToServer() throws IOException
    	{
    		genMsg.setText( "Attempting connection\n" );
    		// create Socket to make connection to server
    		client = new Socket(InetAddress.getByName( chatServer ), serverport );
    		// display connection information
    		genMsg.append( "Connected to: " +client.getInetAddress().getHostName() );
    	}
    
    	private void getStreams() throws IOException
    	{
    		// set up output stream for objects
    		output = new ObjectOutputStream(client.getOutputStream() );
    		// flush output buffer to send header information
    		output.flush();
    	}
    	private void handShake() throws IOException
    	{
    		String message;
    		String value[];
    		value = new String [100];
    		try {
    		output.writeObject(Client_name);
    		output.flush();
    		input = new ObjectInputStream(client.getInputStream() );
    		genMsg.append( "\n Client Name Send"  );
    		message = (String) input.readObject();
    		int i=0;
    		 while ( !message.equals( "." ) ){
    			
    			//genMsg.append("\n"+message );
    			value[i++] =message; 
    			message = (String) input.readObject();
    		}
    		userList.setListData(value);
    		message = (String) input.readObject();
    		genMsg.append("\n"+message );
    		}
    		// process problems sending object
    		catch ( IOException ioException ) {
    			genMsg.append( "\nError writing object" );
    		}
    		 catch ( ClassNotFoundException classNotFoundException ) {
    			System.out.println( "\nUnknown object type received" );
    			}
    	
    	}
    	public static void main( String args[] )	{ 
    	
    		ClientChat application;
    		application = new ClientChat( "127.0.0.1",5130,args[0] );
    		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    		application.runClient();
    	  }
       }
    The Server code

    Java Code:
    // Java core packages
         import java.io.*;
         import java.net.*;
         import java.util.*;
         import java.awt.*;
         import java.awt.event.*;
         import javax.swing.*;
    
         public class ServerChat extends JFrame 
         {
    	private ObjectInputStream input;
    	private	ObjectOutputStream output;
    	private JTextField enterField;
    	public static JTextArea displayArea;
    	private ServerSocket server;
    	private Socket connection;  
    	private ServerThread c1[];
    	//private Client Cname;
    	private static String clientList[];
    	private int counter = 0;
    
    	public ServerChat()
    	{
    		super( "Server" );
    		Container container = getContentPane();
    		clientList = new String[100];
    		// create enterField and register listener
    		enterField = new JTextField();
    		enterField.setEnabled( false );
    		enterField.addActionListener(
    
    		new ActionListener() {
    			// send message to client
    			public void actionPerformed( ActionEvent event )
    			{
    				//sendData( event.getActionCommand() );
    			}
    		} // end anonymous inner class
    		); // end call to addActionListener
    
    		container.add( enterField, BorderLayout.NORTH );
    		// create displayArea
    		displayArea = new JTextArea();
    		container.add( new JScrollPane( displayArea ), BorderLayout.CENTER );
    		setSize( 300, 150 );
    		setVisible( true );
    	}
    	public void runServer()
    	{
    		// set up server to receive connections;
    		// process connections
    		try {
    		// Step 1: Create a ServerSocket.
    		server = new ServerSocket( 5130, 100);
    		c1 = new ServerThread[100];
    		while ( true ) {
    			// Step 2: Wait for a connection.
    			waitForConnection();
    			handShake();
    			displayArea.append("\nHanshake Complete");
    			//c1[counter] = new ServerThread(connection,counter);	
    			//c1[counter].start();
    			++counter;
    			}
    		}
    		// process EOFException when client closes connection
    		catch ( EOFException eofException ) {
    		System.out.println( "Client terminated connection" );
    		}
    		// process problems with I/O
    		catch ( IOException ioException ) {
    		ioException.printStackTrace();
    		}
    	}
    	private void waitForConnection() throws IOException
    	{
    		displayArea.append( "\nWaiting for connection " );
    		// allow server to accept a connection
    		connection = server.accept();
    		displayArea.append( "\nClient connected : Client");
    	}
    	private void handShake() throws IOException
    	{
    		output = new ObjectOutputStream(connection.getOutputStream() );
    		output.flush();
    		String message;
    		input = new ObjectInputStream(connection.getInputStream() );
    		try {
    			message = (String) input.readObject();
    			clientList[counter]= message;
    			displayArea.append( "\nClient connected : "+message);
    			for (int i=0; i<=counter;i++ )
    			{
    				output.writeObject(clientList[i]);
    				output.flush();
    			}
    			output.writeObject(".");
    			output.flush();
    			}
    		 // catch problems reading from client
    		catch ( ClassNotFoundException classNotFoundException ) {
    			System.out.println( "\nUnknown object type received" );
    			}
    			catch ( IOException ioException ) {
    		ioException.printStackTrace();
    		}
    	}
         public static void main( String args[] )
    	{
    		ServerChat application = new ServerChat();
    		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    		application.runServer();
    	}
    	}
    the Server Thread Code

    Java Code:
    import java.io.*;
         import java.net.*;
         import java.awt.*;
         import java.awt.event.*;
         import javax.swing.*;
         public class ServerThread extends Thread 
         {
    	private Socket connection;
    	private ObjectOutputStream output;
    	private ObjectInputStream input;
    	private int counter;
    	public ServerThread(Socket con,int count)
    	{
    		connection = con;
    		counter = count;
    
    	}
    	public void run()
    	{
    			try 
    			{
    				getStreams();
    				processConnection();
    			}
    			// process EOFException when client closes connection
    		catch ( EOFException eofException ) {
    		System.out.println( "Client terminated connection" );
    		}
    
    		// process problems with I/O
    		catch ( IOException ioException ) {
    		ioException.printStackTrace();
    		}
    	}
    		private void getStreams() throws IOException
    		{
    		// set up output stream for objects
    		output = new ObjectOutputStream(connection.getOutputStream() );
    		// flush output buffer to send header information
    		output.flush();	
    		output.writeObject( "SERVER>>> Connection successful");
    		output.flush();
    		}
    	private void processConnection() throws IOException
    	{
    		// send connection successful message to client
    		String message = "SERVER>>> Connection successful";
    		
    		output.writeObject( message );
    		output.flush();
    	}
    	private void closeConnection() throws IOException
    	{
    		ServerChat.displayArea.append( "\nUser terminated connection" );
    		
    		output.close();
    		input.close();
    		connection.close();
    		}
         }
    thank you...
    Last edited by pappyj77; 08-25-2011 at 05:56 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Can you post a more code? Especially code unrelated to the problem.

  3. #3
    pappyj77 is offline Member
    Join Date
    Jul 2011
    Location
    kent, UK
    Posts
    47
    Rep Power
    0

    Default

    please what do you mean by a more code? and how you create that? thank you.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    How about less code. Can you make a small program that has some GUI and a JList and show what your problem is?
    The rest of the code is not needed for solving your problem.

  5. #5
    pappyj77 is offline Member
    Join Date
    Jul 2011
    Location
    kent, UK
    Posts
    47
    Rep Power
    0

    Default

    am sorry, i dont understand, i need some kind of logic or snippet to do this, its not just the GUI problem, thank you

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Can you write a small, complete program that demonstrates your problem? No one wants to go through all the code you posted.

  7. #7
    pappyj77 is offline Member
    Join Date
    Jul 2011
    Location
    kent, UK
    Posts
    47
    Rep Power
    0

    Default

    hi i have reduced the codes... dont know if it is still too much, but i have tried to capture my main concern as much as possible. thank you.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Do you have a script that demonstrates the problem when your code is executed? Something automatic that will not require user intervention.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    The code you posted is missing lots of ending }s.
    It does NOT compile.

  10. #10
    pappyj77 is offline Member
    Join Date
    Jul 2011
    Location
    kent, UK
    Posts
    47
    Rep Power
    0

    Default

    sorry, it took a while, i now understand what you mean. i have edited the code and its running fine, it shows the problem and less bulky now. thank you...
    Quote Originally Posted by Norm View Post
    The code you posted is missing lots of ending }s.
    It does NOT compile.

  11. #11
    pappyj77 is offline Member
    Join Date
    Jul 2011
    Location
    kent, UK
    Posts
    47
    Rep Power
    0

    Default

    help anybody?

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    How do you execute the code to show the problem?

    Do you have a script that demonstrates the problem when your code is executed? Something automatic that will not require user intervention.

  13. #13
    pappyj77 is offline Member
    Join Date
    Jul 2011
    Location
    kent, UK
    Posts
    47
    Rep Power
    0

    Default

    All three programs posted should be in separate files in same folder. compile and run the serverChat first and then compile and run the ClientChat.

    But run the ClientChat with a name like this:(java ClientChat pappyj)

    You have to run the ClientChat with 2 separate names simulteneusly to see the problem.

    thank you for your response.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Please explain what the problem is.
    I have merged the code for ease of testing and am using the following code to test it.
    Java Code:
          Thread t = new Thread(new Runnable() {
             public void run() {
       		ServerChat serverApp = new ServerChat();
       		serverApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
       		serverApp.runServer();
             }
          });
          t.start();
    
          Thread t1 = new Thread(new Runnable() {
             public void run() {
          		ClientChat clientApp;
          		clientApp = new ClientChat( "127.0.0.1", 5130, "Norm"); //args[0] );
          		clientApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
                   clientApp.setVisible(true); //<<<<<<<<<<<<<<
          		clientApp.runClient();
             }
          });
          t1.start();
        	ClientChat clientApp;
            clientApp = new ClientChat( "127.0.0.1", 5130, "Someone"); //args[0] );
    	clientApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
             clientApp.setVisible(true); //<<<<<<<<<<<<<<  NEED THIS
    	clientApp.runClient();
    Now what?

  15. #15
    pappyj77 is offline Member
    Join Date
    Jul 2011
    Location
    kent, UK
    Posts
    47
    Rep Power
    0

    Default

    you will find that while both Client windows are open. Norm would have "Norm" on its userList while someone have "Norm" and "Someone" on its userList.---this is the problem.

    they should both have "Norm" and "Someone" on their userList.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Here is how I would debug your code.
    Add lots of printlns to show the execution flow and how the values of variables change. Looking at the print out should show you what is happening.
    You will need to add printlns for where the data is being displayed to the textarea so that you can keep the events in sequence and all the debug data in one place.

  17. #17
    tanu.kansal is offline Member
    Join Date
    Aug 2011
    Posts
    18
    Rep Power
    0

    Default

    hey paaapy u can see my thread
    there ur problem is solved my code is updating user list

  18. #18
    pappyj77 is offline Member
    Join Date
    Jul 2011
    Location
    kent, UK
    Posts
    47
    Rep Power
    0

    Default

    yes thank you i have done that alot and it has helped me alot. but i am thinking my problem is a logical problem, i have just run out of ideas on what to do.
    I know its a one way communication from client - server -serverthread - client but i need to get coomunication back like client = server = serverthread = client
    thank you.
    Quote Originally Posted by Norm View Post
    Here is how I would debug your code.
    Add lots of printlns to show the execution flow and how the values of variables change. Looking at the print out should show you what is happening.
    You will need to add printlns for where the data is being displayed to the textarea so that you can keep the events in sequence and all the debug data in one place.

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    The ServerThread class is not used in the posted code.

  20. #20
    pappyj77 is offline Member
    Join Date
    Jul 2011
    Location
    kent, UK
    Posts
    47
    Rep Power
    0

    Default

    thats true.... i have commented it out. but all the same i just want to see a reply

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 4
    Last Post: 03-31-2011, 11:37 PM
  2. Replies: 2
    Last Post: 03-21-2010, 03:31 PM
  3. Cannot get my JList to update!
    By rangvald in forum AWT / Swing
    Replies: 1
    Last Post: 11-17-2009, 02:57 PM
  4. Help with Java IRC Chat Channel
    By sari in forum New To Java
    Replies: 1
    Last Post: 02-03-2009, 08:21 AM
  5. Java Program chat
    By susan in forum Advanced Java
    Replies: 1
    Last Post: 07-25-2007, 10:05 PM

Tags for this Thread

Posting Permissions

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