Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-11-2007, 10:02 AM
Member
 
Join Date: Jun 2007
Posts: 3
candide56 is on a distinguished road
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
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

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
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
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
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-12-2007, 02:19 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
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:

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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-13-2007, 12:18 AM
Member
 
Join Date: Jun 2007
Posts: 3
candide56 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-14-2007, 12:43 AM
Member
 
Join Date: May 2007
Posts: 60
Jamie is on a distinguished road
Quote:
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.

Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-14-2007, 02:02 AM
Member
 
Join Date: Jun 2007
Posts: 3
candide56 is on a distinguished road
Thank you so much for your help I shall work on it now
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 11:13 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org