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
here is the send classCode: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 my Receive classCode:
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 ChatServerCode: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
and finally the ChatClientCode:
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
here there areCode: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
please help me with this problem
