I'm making a simple GUI for the chat portion of a project I am working on. I wish to make a chat window for the conversation, but for some reason, 2 extra windows (1 for each thread
, I would assume) pop up every time I run the main method which calls the threads.
So far, I have the following:
package udpcs1;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class UDPCS1 implements ActionListener {
private static String csMsg1 = "";
private static int played = 0;
private static UDPCS1 chat = new UDPCS1();
private final JFrame f = new JFrame();
private final JTextField tf = new JTextField(60);
private final JTextArea textArea = new JTextArea(10, 60);
private final JButton send = new JButton("Send");
protected String[] message = { ... };
protected String[] rps = { ... };
public UDPCS1() {
f.setTitle("Chat");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getRootPane().setDefaultButton(send);
f.add(tf, BorderLayout.NORTH);
f.add(new JScrollPane(textArea), BorderLayout.CENTER);
f.add(send, BorderLayout.SOUTH);
f.setLocation(400, 300);
f.pack();
send.addActionListener(this);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent event) {
String msg = tf.getText();
display(msg);
tf.setText("");
}
// Server/client thread already synchronize their output, so only need to display it
protected void display(final String s){
textArea.append(s + "\n");
}
protected UDPCS1 getChat() {
return chat;
}
// get/set methods for some intra-host communication
protected String getMsg() { ... }
protected void setMsg(String newMsg) { ... }
protected int getNum() { ... }
protected void incNum() { ... }
public static void main(String[] args) throws Exception {
// Start the first server-client pair
UDPServer1 server1 = new UDPServer1();
UDPClient1 client1 = new UDPClient1();
}
}
My server and client both extend this class
, and then call getChat().display(String s)
when they wish to output to the chat window. However, at the same time, I get 3 initial chat windows (possibly exactly because I extend it). However, I do need some functionality from this class so I need it to be a superclass. What could I do to keep the overall functionality the same, without getting 3 chat windows?
PS. I realize that using the static variables is seen as a grave sin, but I couldn't really figure out any other way to have those variables be accessible to both the client and server as easily. Suggestion on ways to improve that are also most welcome.
EDIT: Removing f.setVisible(true);
from within the UDPCS1
constructor, making f
static
, and then calling f.setVisible(true);
from within the UDPCS1
thread (the initial program) seems to solve the issue, as only one chat window appears and both the client and the server communicate with it. However, I am sure there must be a better/less error-prone/cleaner way to do this. I'll still be awaiting any answers and comments.