Results 1 to 5 of 5

Thread: ChatGUI problem

  1. #1
    candide56 is offline Member
    Join Date
    Jun 2007
    Posts
    3
    Rep Power
    0

    Red face ChatGUI problem

    I am struggling with my ChatGUI please could you help me
    I created two classes ChatServer and ChatClient
    and in a package called Chatpack I created a ChatGUI class, a Send class, a Receive class
    however when I type a message and click the send button nothing happens
    indeed I do not know what to put inside the send.addActionListener in the ChatGUI
    here it is
    Java Code:
    package Chatpack;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ChatGUI extends JFrame
    {
    	public static JTextField inputText;
    
    	public static JTextArea message;
    
    	public static JButton exit, send;
    
    	String lastMessageSent;	
    	private boolean available = false;
       
        	public ChatGUI()
    	{
    	String lastMessageSent = " ";
                 send = new JButton("Send");
            	
    	send.addActionListener(new ActionListener()
    	{
    	        public void actionPerformed(ActionEvent e)
    	       {
    				
    	        }	// close the actionPerformed(ActionEvent e)
    	});
                	exit = new JButton("Exit");
            	
    	exit.addActionListener(new ActionListener()
    	{
    	        public void actionPerformed(ActionEvent e)
    	       {
    	           System.exit(0);
    	       }	// close the actionPerformed(ActionEvent e)
    	});
                  
    	inputText = new JTextField(40);
    	inputText.setBackground(Color.WHITE);
    		
    	inputText.addActionListener(new ActionListener()
    	{
    	         public void actionPerformed(ActionEvent e)
    	        {
    	              String lastMessageSent = inputText.getText();
    	              postMessage("Sent: " + lastMessageSent);	            	
                          }	// close the actionPerformed(ActionEvent e)
    	});	// close the add.ActionListener
    	
    	message = new JTextArea();
            	message.setLineWrap(true);
    	message.setWrapStyleWord(true);
    	message.setEditable(false);
    	message.setBackground(Color.WHITE);
    	JScrollPane scroller = new JScrollPane(message);
    	setPreferredSize( new Dimension(650,450)); 
    	setBackground(Color.GRAY);                
    	setLayout(new BorderLayout(5,5));           
    	JPanel bottom = new JPanel();
    	bottom.setBackground(Color.GRAY);
            	bottom.add(inputText);
            	bottom.add(send);
    	JPanel top = new JPanel();
    	top.setBackground(Color.GRAY);
            	top.add(exit);
    	add(bottom, BorderLayout.SOUTH);
            	add(scroller, BorderLayout.CENTER);
    	add(top, BorderLayout.NORTH);
    	setTitle("MyChat Program");
    	}	// close the ChatGUI constructor
    
    	synchronized public String getInputText()
    	{
    	       while(available == false)
    	      {
    	           try
    	          {
    	                wait();
    	          }
    	          catch (InterruptedException e)
    	          {
    	          }
    	      }	// close while block
    	       available = false;
    	       notifyAll();
    	       return inputText.getText();
    	}	// close getInputText method
    
    	synchronized public void setInputText(String inputText)
    	{
    	        while (available == true)
    	        {
    	            try 
    	           {
    	                wait();
    	           }
    	           catch (InterruptedException e)
    	          {
    	           }
    	          }	// close while block
    	          available = true;
    	          notifyAll();
    		
    		
    	}	// close setInputText(String inputText) method
    	
    	synchronized private void sendMessage(String lastMessageSent) 
    	{
    	          message.append(lastMessageSent + "\n");
    	}
                 public void actionPerformed(ActionEvent e)
    	{
    	         String lastMessageSent = inputText.getText();
    	         message.append(lastMessageSent + "\n");
    	}	// close the actionPerformed(ActionEvent e)
    }	// close ChatGUI class
    here is the send class

    Java Code:
    package Chatpack;
    
    import java.io.*;
    import java.net.Socket;
    
    
    public class Send extends Thread
    {
    	private Socket sock;
    	private String lastMessageSent;
    	
    	private PrintWriter writer;
                 public ChatGUI myGUI;
    
         	
        	public Send(ChatGUI gui, Socket socket)
    	{
    	         sock = socket;
    	         String lastMessageSent = "";
    	         myGUI = gui;
    	         try
    	        {
    	              writer = new PrintWriter(socket.getOutputStream());
    	         }	// close try block
        	         catch(IOException ioe)
        	         {
           	                System.out.println("Could not create output stream from socket");
           	                ioe.printStackTrace();
    	         }	// close catch(Exception ioe)
    
             	
    	}	// close Send(ChatGUI gui, Socket socket) constructor
            
    	public void run()
    	{
    	         while(true)
    	        {
    	              if(!(lastMessageSent.equals(myGUI.lastMessageSent)))
    	             {
    	                   String lastMessageSent = myGUI.lastMessageSent;
    	                   writer.println(lastMessageSent);	
                                    writer.flush(); 
    	             }	//close if block
                              try
    	             {
    	                      sleep(500);
    	             }
    	             catch(java.lang.InterruptedException ie) 
    	            {
    	                           ie.printStackTrace();
    	            }
    
    	        }	// close while loop
    		 	
    	}	// close run method
    	
    	public void finalize()
    	{
        	            try
    	            {
    	                      writer.close();
    	                      sock.close();
    	            }	// close try block
    	            catch (IOException e)
    	           {
    	           }	            // catch (IOException e)
    	}	// close void finalize
    
    
    }	// close Send class
    here is my Receive class
    Java Code:
    package Chatpack;
    
    import java.net.Socket;
    import java.io.*;
    
    public class Receive extends Thread
    {
    		
    	private String lastMessageSent;
    
    	private BufferedReader bReader;
    	
    	private Socket sock;
    		
    	public ChatGUI myGUI; 
    
    	public Receive(ChatGUI gui, Socket socket)
    	{
    	           sock = socket; 
    	           lastMessageSent = "";
                           myGUI = gui;
    	          try
    	         {
    	                   bReader = new BufferedReader( new InputStreamReader socket.getInputStream()));
    	         }	// close try block
        	         catch(IOException ioe)
        	        {
           	                 System.out.println("Could not create input stream from socket");
           	                 ioe.printStackTrace();
    	         }	// close catch(Exception ioe)
    	}	// close receive (ChatGUI gui, Socket socket)constructor
    	public void run()
    	{
    	          try
    	          {
    	                 while(true)
    	                {
    	                           lastMessageSent = bReader.readLine() + "\n";
    	                           try
    	                          {
    	                                   sleep(500);
    	                          }
                                 	 catch(java.lang.InterruptedException ie) 
                                           {
    	                                   ie.printStackTrace();
    	                           }
    	                   }     // close while loop
             		 catch(IOException e)
    	             {
    	             }	// close catch (IOException e)				}	// close run method
    	public void finalize()
    	{
        	          try
    	          {
    	                    bReader.close();
    	                   sock.close();
    	           }	// close try block
    	          catch(IOException e)
        	          {
           	          }	// close catch(Exception e)
    	}	// close void finalize
    
    }	// close Receive class
    here is my ChatServer
    Java Code:
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.io.*;
    
    import Chatpack.*;
    
    
    public class ChatServer
    {
    	
            private static final int intPortNumber = 4321;
     	
    	
                ChatGUI myGUI;
    	Send mySend;
    	Receive myReceive;
    
    	public ChatServer(ChatGUI gui, Send send, Receive receive)
    	{	
    	myGUI = gui;
    	mySend = send;
    	myReceive = receive;
    	}	// close ChatServer(ChatGUI gui, Send send, Receive receive) constructor
    		
    	public static void main(String args[]) 
    	{
            	Socket clientSocket = null;
      	ServerSocket serverSocket = null; 
    	clientSocket = null;
    	try
    	{
    	serverSocket = new ServerSocket(intPortNumber);
    	clientSocket = serverSocket.accept();
    	}	// close try block
    	catch (IOException e)
    	{
    	System.err.println("Could not listen on port: 4321.");
    	System.err.println(e.getMessage());
    	System.exit(1);
    	}	// close catch(IOException e)
       	ChatGUI gui = new ChatGUI();
    	Send send = new Send(gui, clientSocket);
    	Receive receive = new Receive(gui, clientSocket);
    	send.start();
    	receive.start();
    	}	//close main method
            
    }	//close ChatServer class
    and finally the ChatClient
    Java Code:
    import java.net.Socket;
    import java.io.*;
    
    import Chatpack.*;
    
    public class ChatClient
    {
    	
    	private static final int intPortNumber = 4321;	 
    	private static String address;
    	
    	ChatGUI myGUI;	
    	Send mySend;
    	Receive myReceive;
    
    	public ChatClient(ChatGUI gui, Send send, Receive receive)
    	{	
    	myGUI = gui;
    	mySend = send;
    	myReceive = receive;
    	}	// close ChatClient(ChatGUI gui, Send send, Receive receive) constructor
    	
    	public static void main(String args[])
                {
    	Socket clientSocket = null;
    	clientSocket = null;
    	BufferedReader keyboardReader = null;
    	try
    	{
    	clientSocket = new Socket(address,intPortNumber);
    	keyboardReader = new BufferedReader(new InputStreamReader(System.in));
                System.out.print("Enter the IP address for the server: ");
                address = keyboardReader.readLine();
    	}	// close try block
    	catch (IOException ioe)
    	{
    	System.out.println("There was an Input/output problem");
    	System.out.println(ioe.getMessage());
    	System.exit(1);
    	}	// close catch(IOException) block
    
    	ChatGUI gui = new ChatGUI();
    	Send send = new Send(gui, clientSocket);
    	Receive receive = new Receive(gui, clientSocket);
    	send.start();
    	receive.start();
    	
    	}	//close main method
    
    }	//close ChatClient class
    here there are
    please help me with this problem
    Last edited by candide56; 06-12-2007 at 10:57 AM. Reason: change in the code

  2. #2
    levent Guest

    Default

    Hi

    First of all you don't need these lines in your ChatClient. So i commented them. Those lines block the GUI without any reason since you already know ip address of the server:

    Java Code:
    keyboardReader = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter the IP address for the server: ");
    address = keyboardReader.readLine();
    After commenting those lines, your code will work and both sides will send whatever you wrote in the textfield continuously. But i guess you dont want this, you want to send the data only when the user clicks send button. For that aim you need to modify run method of your Send class. You can define a boolean flag there. When user clicks to the "Send" button, you can set this flag on and thread only send content of the textfield when this flag is on.

    Let us know if something is unclear or you have other problems.
    Last edited by levent; 06-12-2007 at 02:22 PM.

  3. #3
    candide56 is offline Member
    Join Date
    Jun 2007
    Posts
    3
    Rep Power
    0

    Smile

    Thank you very much for your help. I removed the lines you suggested but Cannot figure out how to do the boolean flag.
    In the ChatGUI I left empty the
    send.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {

    because I do not know what to put in it.
    in addition I cannot run the chat anymore as
    I have this
    Exception in thread "Thread-2" java.lang.NullPointerException
    at Chatpack.Send.run(Send.java:63)
    Could you help me with this
    again thank you

  4. #4
    Jamie is offline Member
    Join Date
    May 2007
    Posts
    60
    Rep Power
    0

    Default

    because I do not know what to put in it.
    in addition I cannot run the chat anymore as
    I have this
    Exception in thread "Thread-2" java.lang.NullPointerException
    Then do not remove those lines. I guess the problem was related to a netbeans bug. Same happened to me. It just did not ask me the ip address, then i removed those lines and it worked without those lines.

    Cannot figure out how to do the boolean flag.
    I noticed that you have a different logic there which should work too. You just seem to use "lastMessageSent" variable in Send class wrongly.

    You already defined it as a class variable at the top of your class. When you use "String lastMessageSent = "";" and "String lastMessageSent = myGUI.lastMessageSent;" in your methods, you are defining a new variable called lastMessageSent. So do not use String keyword before your variable name while using it inside methods.

    Good luck.

  5. #5
    candide56 is offline Member
    Join Date
    Jun 2007
    Posts
    3
    Rep Power
    0

Posting Permissions

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