Search code examples
javaswingswingworker

SwingWorker.process does not get called


Edit: I solved my Problem:

while(!finish.equals("readline")){
    publish(finish);
    finish = Main.in.readLine();
}

has to be changed to:

while(true){
    publish(finish);
    finish = Main.in.readLine();
}

The old Question:

I am writing a chat application in Java, i have made a simple Gui and it is working fine. But now i have rewritten the part of collecting Chat messages to be thread-safe. On login, the server is pushing all messages aready in chat to the client, this works well. After this moment, the GUI keeps responding, i can send commands and they get to the server but the process-method does not get called, so there is nothing returned. What am i doing wrong?

Here is the code i am talking about:

package Client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;

import javax.swing.SwingWorker;

public class ChatReceiver extends SwingWorker<Void, String> {
    @Override
    protected Void doInBackground() {
        try {
            Main.echoSocket = new Socket(Main.ip, 4444);
            Main.out = new PrintWriter(Main.echoSocket.getOutputStream(), true);
            Main.in = new BufferedReader(new InputStreamReader(
                    Main.echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host.");
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                    + "the connection to: localhost.");
        }
        publish("Sending Login-Request...");
        Main.out.println("login");
        try {
            String text = Main.in.readLine();
            String status = Main.in.readLine();
            publish("Receiving: " + text + " " + status);
            if(status.equals("readline") && text.equals("ready")){
                String finish = "logging in...";
                Main.out.println(Main.name + " SZUTKEY");
                while(!finish.equals("readline")){
                    publish(finish);
                    finish = Main.in.readLine();
                }
            }else{
                throw new RuntimeException("Fuck it login died!");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void process(List<String> chats) {
        Main.printTest("xxx");
        for(String s : chats.toArray(new String[0])){
            Main.printTest("called");
            Main.printTest(s);
        }
        Main.textField.validate();
        Main.textField.repaint();
    }
}

this gets called in my Gui:

    loginAction  = new AbstractAction("Login") {

        /**
         * 
         */
        private static final long serialVersionUID = -3279591081543889275L;

        @Override
        public void actionPerformed( ActionEvent arg0 ) {
            name = JOptionPane.showInputDialog("Please input your Chatname");
            ip = JOptionPane.showInputDialog("Please input the Chatserver-IP", "localhost");
            (chat = new ChatReceiver()).execute();
        }
    };

Solution