Arcthor
Arcthor

Reputation: 63

Java - invokeLater will not run

I followed a tutorial to create a messenger. I typed the code correctly, and I have pretty basic understanding of what everything does.

Although, I do not end up with the same result. Here's the code:


public class Server extends JFrame{
    private JTextField userText;
    private JTextArea chatWindow;
    private ObjectOutputStream output;
    private ObjectInputStream input; 
    private ServerSocket server;
    private Socket connection;

    //constructor
    public Server(){
        super("Coffee Messenger");
        userText = new JTextField();
        userText.setEditable(false);
        userText.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    sendMessage(event.getActionCommand());
                    userText.setText("");
                }
            }
        );
        add(userText, BorderLayout.SOUTH);
        chatWindow = new JTextArea();
        add(new JScrollPane());
        setSize(300,150);
        setVisible(true);   
    }
    //set up and run the server
    public void startRunning(){
        try{
            server = new ServerSocket(6789, 100);
            while(true){
                try{
                    //connect and have conversation
                    waitForConnection();
                    setupStreams();
                    whileChatting();
                }catch(EOFException eofException){
                    showMessage("\n Server ended the connection!");
                }finally{
                    closeCrap();
                }
            }
        }catch(IOException ioException){
            ioException.printStackTrace();
        }
    }
    //wait for connection, then display connection information
    private void waitForConnection() throws IOException{
        showMessage("Waiting for someone to connect...\n");
        connection = server.accept();
        showMessage("Now connected to " + connection.getInetAddress().getHostName());
    }
    //get stream to send and receive data
    private void setupStreams() throws IOException{
        output = new ObjectOutputStream(connection.getOutputStream());
        output.flush();
        input = new ObjectInputStream(connection.getInputStream());
        showMessage("\n Streams are now setup! \n");
    }
    //during the chat conversation
    private void whileChatting() throws IOException{
        String message = "You are now connected!";
        sendMessage(message);
        ableToType(true);
        do{
            //have conversation
            try{
                message = (String) input.readObject();
                showMessage("\n " + message);
            }catch(ClassNotFoundException classNotFoundException){
                showMessage("\n idk wtf that user sent");
            }
        }while(!message.equals("CLIENT - END"));
    }
    //close streams and sockets (application)
    private void closeCrap(){
        showMessage("\n Closing connections...\n");
        ableToType(false);
        try{
            output.close();
            input.close();
            connection.close();
        }catch(IOException ioException){
            ioException.printStackTrace();
        }
    }
    //send message to client
    private void sendMessage(String message){
        try{
            output.writeObject("SERVER - " + message);
            output.flush();
            showMessage("\nSERVER - " + message);

        }catch(IOException ioException){
            chatWindow.append("\n ERROR: Cannot send message.");
        }
    }
    //updates chatWindow 
    private void showMessage(final String text){
        SwingUtilities.invokeLater(
            new Runnable(){
                public void run(){
                    chatWindow.append(text);
                }
            }
        );
    }
    //sets the ability to edit the textfield
    private void ableToType(final boolean tof){
        SwingUtilities.invokeLater(
            new Runnable(){
                public void run(){
                    userText.setEditable(tof);
                }
            }
        );
    }
}
__

When I start the application from my main method, the string "Waiting for someone to connect" (from the waitForConnection method) does not show up. I believe the problem lies within the showMessage method. Am I using it wrong? If I replace the invokeLater method with a simple system.out.println();, the project runs exactly as planned.

Sorry, I'm a bit inexperienced so it might be something really simple. Thank you very much in advance.

(credits to thenewboston for making these tutorials)

Upvotes: 1

Views: 91

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Here:

chatWindow = new JTextArea();
add(new JScrollPane());

your GUI is creating a JTextArea, chatWindow, but adding it to nothing that displays it, and instead your GUI displaying an empty JScrollPane. It would be much better if you had,

chatWindow = new JTextArea();
add(new JScrollPane(chatWindow));

This way text sent to the JTextArea has a chance of being displayed.

Upvotes: 2

Related Questions