0

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.

CoderNeji
  • 2,056
  • 3
  • 20
  • 31
filpa
  • 3,651
  • 8
  • 52
  • 91
  • What do you mean by "create a **static** chat object"? If you mean use a static variable or singleton, then don't do this. Otherwise, I think that you're going to want to experiment with your code some, if only to allow you to be able to ask more specific question on this topic. This is not a great site for "any suggestions" type questions, but rather it is more of a specific question specific solution type site. – Hovercraft Full Of Eels Jul 23 '13 at 15:52
  • Let me rephrase: I meant to create a chat window (chat object) that both server and client would have access to. I added static because it would be the same for both server and client, hence they would add to the same chat. Is this thinking flawed? EDIT: I noted your edit. I"ll be going over my code a little bit more and see if I can come up with something more specific. My apologies! – filpa Jul 23 '13 at 15:54
  • Very much so. You never want to use static anything when simply passing a correct reference is indicated. – Hovercraft Full Of Eels Jul 23 '13 at 15:55
  • For reference, there's a complete example [here](http://stackoverflow.com/a/3245805/230513). – trashgod Jul 23 '13 at 16:09
  • @trashgod Thank you very much for that example, it helped me immensely. @HovercraftFullOfEels I've updated the question to be more pertinent to what I specifically wish to do now. I just hope I don't step on any toes with my ineptitude regarding `static`... – filpa Jul 23 '13 at 19:00
  • @Hovercraft's maxim still applies, and it offers yet another reason why [multiple frames are bad](http://stackoverflow.com/q/9554636/230513). Purely for demonstration purposes, my example uses `enum` to [enforce the singleton property](http://stackoverflow.com/a/4709813/230513). – trashgod Jul 23 '13 at 21:39
  • Your code should not contain line numbers. Can you please remove these? – Hovercraft Full Of Eels Jul 23 '13 at 21:53

1 Answers1

0

Just remove the static keyword from private static UDPCS1 chat = new UDPCS1(); and no more chat windows will be there... Hoping it helped...

Helped in my case i was doing something too,...

CoderNeji
  • 2,056
  • 3
  • 20
  • 31