Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Need Help With A Simple Chat Program

  Asked By: Ella    Date: Jan 19    Category: Java    Views: 1430
  

I'm fairly new to Java and I have a quick question regarding a
simple chat program in java. My problem is that I have a simple chat
program that runs from its own JFrame etc. Most of you are probably
familiar with the code below, i got it from one of my java books. In
any case, what I'm attempting to do is integrate this chat pane into
a gui that i have created. I attempted to call an instace of the
Client class from my gui program so that I can use the textfield and
textarea contained in my app, but it will not allow me to do it.
Would I need to integrate this code into the code for my Gui class.
I have a simple program that contains chat and a game. The code for
the Client is listed below.

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

import javax.swing.*;

public class Client
extends JPanel {


public static void main(String[] args) throws IOException {
String name = args[0];
String host = args[1];
int port = Integer.parseInt(args[2]);

final Socket s = new Socket(host, port);
final Client c = new Client(name, s);

JFrame f = new JFrame("Client : " + name);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
c.shutDown();
System.exit(0);

}
});
f.setSize(300, 300);
f.setLocation(100, 100);
f.setContentPane(c);
f.setVisible(true);
}

private String mName;
private JTextArea mOutputArea;
private JTextField mInputField;
private PrintWriter mOut;


public Client(final String name, Socket s)
throws IOException {
mName = name;
createUI();
wireNetwork(s);
wireEvents();
}

public void shutDown() {
mOut.println("");
mOut.close();
}

protected void createUI() {
setLayout(new BorderLayout());
mOutputArea = new JTextArea();
mOutputArea.setLineWrap(true);
mOutputArea.setEditable(false);
add(new JScrollPane(mOutputArea), BorderLayout.CENTER);
mInputField = new JTextField(20);
JPanel controls = new JPanel();
controls.add(mInputField);
add(controls, BorderLayout.SOUTH);
mInputField.requestFocus();
}

protected void wireNetwork(Socket s) throws IOException {
mOut = new PrintWriter(s.getOutputStream(), true);

final String eol = System.getProperty("line.separator");
new Listener(s.getInputStream()) {
public void processLine(String line) {
mOutputArea.append(line + eol);
mOutputArea.setCaretPosition(
mOutputArea.getDocument().getLength());
}
};
}

protected void wireEvents() {
mInputField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String line = mInputField.getText();
if (line.length() == 0) return;
mOut.println(mName + " : " + line);
mInputField.setText("");
}
});
}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Need Help With A Simple Chat Program Or get search suggestion and latest updates.




Tagged: