Search code examples
javauser-interfaceclientchatsend

How to implement a send button in a chat client GUI


I have implemented a chat client. And as of now it only fully works in cmd, because I can't figure out how I'm supposed to implement the send button in the chat client GUI.

So this is my chat client class:

class ChatClientClass {

    private String host;
    private int port;
    private Socket socket;
    private PrintWriter out;
    private BufferedReader in;
    private BufferedReader stdIn;

    ChatWindow chatWindow = new ChatWindow();

    public ChatClientClass(String host, int port) {
        try {   
            this.socket = new Socket(host, port);

            this.out = 
                new PrintWriter(socket.getOutputStream(), true);
            this.in = 
                new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));
            this.stdIn =
                new BufferedReader(
                    new InputStreamReader(System.in));

            String userInput;
            while ((userInput = stdIn.readLine()) != null) {
                out.println(userInput);
                chatWindow.addText("You: " + userInput);
                String serverMessage = in.readLine();
                chatWindow.addText(serverMessage);
            }
        } catch (UnknownHostException e) {
            System.err.println("Couldn't not find host " + host);
            e.printStackTrace();
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " + host);
            e.printStackTrace();
            System.exit(1);
        }
    }   
}

As of now, I can only write stuff in the command prompt. But what I've written as well as what the server answers will appear in my GUI. The addText(String text)-method adds the input and the output to my GUI.

But I can't figure out how to implement my send button. An easy way would be if I could just send a reference to the PrintWriter and a reference to the GUI when I call the constructor of my ActionListener class, and just do something like: thePrintWriter.println( [get the value of the text area that belongs to the send button] ) in the public void actionPerformed(ActionEvent e)-method. But since I can't/shouldn't call the constructor of my ActionListener from my chat client class, hence sending those references. That wont be possible, right?

I also thought about making the PrintWriter variable static, and also making the JTextArea containing the message I want to send variable static, and then create static methods to access these two variables. But it just feels like I'm doing something terribly wrong when I'm doing that. And I can't get that to work either.

So how is a send button in a chat client supposed to be implemented?

Thanks in advance!


Solution

  • If you are new in GUI building in java/eclipse. I suggest you the gui builder: http://www.eclipse.org/windowbuilder/

    Its really easy to use, and you can make simple GUI-for your app.

    To your problem you will need a button to your frame, and you need to add an actionlistener than if you fire it you can do what you want.

        import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class ButtonAction {
    
        private static void createAndShowGUI()  {
    
            JFrame frame1 = new JFrame("JAVA");
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JButton button = new JButton(" >> JavaProgrammingForums.com <<");
            //Add action listener to button
    
    
    
            button.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e)
                {
                    //Execute when button is pressed
                     //Here call your sender fucntion 
               out.println(userInput);
               chatWindow.addText("You: " + userInput);
               String serverMessage = your_jtext.getText();
               chatWindow.addText(serverMessage);
                    System.out.println("You clicked the button");
                }
            });      
    
            frame1.getContentPane().add(button);
            frame1.pack();
            frame1.setVisible(true);
        }
    
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }