Hi. I have been giving a program which I need to use to make a GUI for the program. The program is called a Chat Program. For the program ... I need to make a GUI that included the following Textfields "My Port", "Remote host", "Port", "Input, and "Output" and the Buttons I need to include are "Start Listening", "Connect", "Disconnect", and "Quit". The only part of the GUI that I got are the input, output, and connect. This program is actually like a instant messanger so I have to have 2 windows with the game GUI that will send messages back and forth to each other. I have the ChatEngine.java and ChatListener.java programs. I am just having problems with the GUI and connecting the paramters from the ChatEngine to the GUI program. I am gonna paste all three of the programs so you can see what I am talking about and I am also getting 2 problems in the GUI program.
Here the following programs:
--------------------------
----------
----------
----------
----------
----------
----------
----------
------
ChatEngine.java
--------------------------
----------
----------
----------
----------
----------
----------
----------
------
import java.net.*;
import java.io.*;
public class ChatEngine {
protected int port=8080;
protected String host="127.0.0.1";
private PrintStream out;
private HandleInputSocket socketThread;
public ChatEngine() {
}
public void startListening(int my_listen_port) {
System.out.println("ChatEn
gine.start
Listening(
" + my_listen_port + ")");
try {
socketThread = new HandleInputSocket(my_liste
n_port);
} catch (Exception e) {
System.out.println("Except
ion 0: " + e);
}
}
public void connect(String host, int port) {
System.out.println("ChatEn
gine.conne
ct(" + host + ", " + port + ")");
this.port=port;
this.host = host;
try {
Socket s = new Socket(host, port);
out = new PrintStream(s.getOutputStr
eam());
} catch (Exception e) {
System.out.println("Except
ion 1: " + e);
}
// just for testing:
if (chatListener != null) {
chatListener.receiveText("
This is\na test!\nLogging on\n");
}
}
public void logout() {
try { out.close(); } catch (Exception e) { }
socketThread.stop();
socketThread = null;
if (chatListener != null) {
chatListener.receiveText("
This is\na test!\nLogging off\n");
}
}
public void send(String s) {
try {
out.println(s);
} catch (Exception e) { }
}
void registerChatListener(ChatL
istener cl) {
this.chatListener = cl;
}
protected ChatListener chatListener = null;
// inner class to handle socket input:
class HandleInputSocket extends Thread {
int listenPort = 8191;
public HandleInputSocket(int port) {
super();
listenPort = port;
start();
}
public void run() {
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(listenPort, 2000);
} catch (IOException e) {
System.out.println("Error in handling socket input: " + e + ", port=" + port);
return;
}
try {
while (true) {
Socket socket = serverSocket.accept();
new MyServerConnection(socket)
;
}
} catch (IOException e) {
System.out.println("Error in new socket connection: " + e);
} finally {
try {
serverSocket.close();
} catch (IOException e) {
System.out.println("I/O exception: " + e);
}
}
}
// an inner class to handle incoming socket connections
public class MyServerConnection extends Thread {
protected transient Socket client_socket;
protected transient BufferedReader input_strm;
protected transient PrintStream output_strm;
public MyServerConnection(Socket client_socket) {
this.client_socket = client_socket;
try {
input_strm = new BufferedReader(new InputStreamReader(client_s
ocket.getI
nputStream
()));
output_strm = new PrintStream(client_socket.
getOutputS
tream());
}
catch (IOException io_exception) {
try { client_socket.close(); } catch (IOException io_ex2) { };
System.err.println("Except
ion 2: getting socket streams " +
io_exception);
return;
}
this.start();
}
public void run() {
String input_buf;
try {
while (true) {
input_buf = input_strm.readLine();
if (input_buf == null) {
logout();
break;
}
System.out.println("receiv
ed on socket: " + input_buf);
if (chatListener != null) {
chatListener.receiveText(i
nput_buf);
}
}
}
catch (Exception exception) { }
finally {
try {
client_socket.close();
}
catch (Exception exception) { };
}
}
}
}
}
--------------------------
----------
----------
----------
----------
----------
----------
----------
-------
ChatListener.java
--------------------------
----------
----------
----------
----------
----------
----------
----------
-------
public interface ChatListener {
public void receiveText ( String s );
}
--------------------------
----------
----------
----------
----------
----------
----------
----------
-------
SwingGUI.java
--------------------------
----------
----------
----------
----------
----------
----------
----------
-------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public abstract class SwingGUI extends JFrame implements ChatListener
{
private JTextArea TextA = new JTextArea();
private JTextArea TextB = new JTextArea();
protected ChatEngine chatEngine = new ChatEngine();
public SwingGUI()
{
chatEngine.registerChatLis
tener(this
);
setTitle("Chat Program");
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
setSize(300,300);
JPanel textPanel = new JPanel();
textPanel.setBackground(Co
lor.black)
;
TextA = new JTextArea(10,20);
TextA.setBackground(Color.
white);
textPanel.add(TextA);
JScrollPane scrolledText = new JScrollPane(TextA);
textPanel.add(scrolledText
);
contentPane.add(textPanel,
BorderLayout.NORTH);
JPanel textPanel2 = new JPanel();
TextB= new JTextArea(3,20);
TextB.setBackground(Color.
white);
textPanel2.add(TextB);
textPanel2.setLayout(new FlowLayout());
contentPane.add(textPanel2
, BorderLayout.CENTER);
JScrollPane scrolledText3 = new JScrollPane(TextB);
textPanel2.add(scrolledTex
t3);
contentPane.add(textPanel2
, BorderLayout.CENTER);
JButton connectButton = new JButton("Connect");
chatEngine.connectButton(c
onnect);
contentPane.add(connectBut
ton,Border
Layout.SOU
TH);
}
public void actionPerformed(ActionEven
t e)
{
String actionCommand = e.getActionCommand();
if(actionCommand.equals("E
xit Chat"))
System.exit(0);
else if(actionCommand.equals("C
lear Text"))
TextA.setText("");
else
TextA.setText("Error in chat interface");
}
public static void main(String[] args)
{
SwingGUI guiChat = new SwingGUI();
guiChat.setVisible(true);
}
}
--------------------------
----------
----------
----------
----------
----------
----------
----------
--------
SwingGUI.java is the only part that I need to work with all the Buttons that I want in there and it is getting me confused how to do it. I hope someone out there would help me in my problem. Thank you again for all your help.