在JavaFx中初始化控制器之前显示阶段

时间:2018-12-04 07:51:34

标签: java multithreading sockets javafx model-view-controller

我使该服务器正常工作,但仅在客户端尝试建立连接时才显示自身。如果我单独执行它,它将在不显示任何内容的情况下运行。这是主文件:

public class Server extends Application {

@Override
public void start(Stage stage) throws Exception {
    FXMLLoader sLoader = new FXMLLoader(getClass().getResource("server.fxml"));

    BorderPane root = new BorderPane(sLoader.load());

    ServerController sController = sLoader.getController();

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    sController.initModel();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

这是它的控制者:

public class ServerController {

@FXML
private TextArea textarea;

public void initModel() {
    try {
        int i = 1;
        ServerSocket s = new ServerSocket(5000);
        //while (true) {
        Socket incoming = s.accept(); //is waiting for connections
        textarea.setText("Waiting for connections");
        Runnable r = new ThreadedEchoHandler(incoming, i);
        new Thread(r).start();
        i++;
        //}
    } catch (IOException e) {
        e.printStackTrace();
    }
}

class ThreadedEchoHandler implements Runnable {

    private Socket incoming;
    private int counter;

    /**
     * Constructs a handler.
     *
     * @param i the incoming socket
     * @param c the counter for the handlers (used in prompts)
     */
    public ThreadedEchoHandler(Socket in, int c) {
        incoming = in;
        counter = c;
    }

    public void run() {
        textarea.setText("Connected from: " + incoming.getLocalAddress());
        String nomeAccount = "";
        try {
            //PHASE 1: The server receives the email
            try {
                BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
                nomeAccount = in.readLine();
            } catch (IOException ex) {
                System.out.println("Not works");
            }

            //PHASE 2: I'm getting all the emails from the files
            File dir = new File("src/server/" + nomeAccount);
            String[] tmp = new String[100];
            int i = 0;
            for (File file : dir.listFiles()) {
                if (file.isFile() && !(file.getName().equals(".DS_Store"))) {
                    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
                        String line;
                        while ((line = br.readLine()) != null) {
                            tmp[i++] = line;
                        }
                    } catch (IOException ex) {
                        System.out.println("Cannot read from file");
                    }
                }
            }

            //PHASE 3: The server sends the ArrayList to the client
            PrintWriter out = new PrintWriter(incoming.getOutputStream(), true);
            for (int j = 0; j < i; j++) 
                out.println(tmp[j]); // send the strings name to client
        } catch (IOException ex) {
            System.out.println("Cannot send the strings to the client");
        } finally {
            try {
                incoming.close();
            } catch (IOException ex) {
                System.out.println("Cannot closing the socket");
            }
        }
    }
}
}

我正在尝试遵循MVC模式,但是我不知道是否应该将等待连接的部分与initialize方法分开。 编辑:textarea.setText("Waiting for connections");也无效,我想是因为编译器到达该部分时textarea仍然不存在

0 个答案:

没有答案