Home > Mobile >  JavaFX FadeTransition end other SetVisbilities stop working when I call a new method
JavaFX FadeTransition end other SetVisbilities stop working when I call a new method

Time:11-10

I want to make a FadeTransition within a pane in my application. Also, with this FadeTransition I set the visibitilitys of some JavaFX inside the pane to false, to make them disappear. It's working fine but, when I call another method that I named waitForResponse(event) after the FadeTransition it just stops working. I don't know why. If I comment the waitForResponse(event) the FadeTransitions start working again. I've thought that maybe it's a problem with the Socket and the InputStreamReader inside the waitForResponse(event), but I tested taking it out and making another basic thing inside this method still not work. I've made other tests and dicovered that FadeTransition and other visibility changes doesn't work if I put any bufferedReader, other loops ou decision structures after it. I just want to make a loading screen that prevent user to click anywhere until it's finished.

This is the code:

public class LoadingScreenController implements Initializable {

    // Socket que vai ser utilizado nos vários métodos para conversar com o servidor
    private Socket cliente;

    // PrintWriter que vai ser utilizado pelos vários métodos e vai passar o
    // argumento para o switch case
    private PrintWriter pr;
    
    private InputStreamReader in;
    
    private BufferedReader bf;
    
    private String option;
    
    private String response;

    @FXML
    private Button refreshButton;

    @FXML
    private ImageView loadingGif;

    @FXML
    private Label txtLabel;
    
    @FXML
    private AnchorPane rootPane;

    public String getOption() {
        return option;
    }

    public void setOption(String option) {
        this.option = option;
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }
    
    @FXML
    private void makeFadeInTransition() {
        FadeTransition fadeTransition = new FadeTransition(Duration.seconds(1), loadingGif);
        fadeTransition.setFromValue(0.0);
        fadeTransition.setToValue(1.0);
        fadeTransition.play();
    }

    @FXML
    private void onRefreshButtonAction(ActionEvent event) {
        if (option == null) {
            throw new IllegalStateException("Entity was null");
        }
        refreshButton.setVisible(false);
        refreshButton.setDisable(true);
        txtLabel.setVisible(false);
        makeFadeInTransition();
        sendOptionToServer(event);
    }
    
    @FXML
    private void sendOptionToServer(ActionEvent event) {
        
            try {
                cliente = new Socket("localhost", 3322);
                pr = new PrintWriter(cliente.getOutputStream());
                in = new InputStreamReader(cliente.getInputStream());
                bf = new BufferedReader(in);
                pr.println(option);
                pr.flush();
                waitForReponse(event, bf);
            } catch (IOException e) {
                e.printStackTrace();
            }
            
    }
    
    private void waitForReponse(ActionEvent event, BufferedReader bf) throws IOException {
        response = bf.readLine();
        switch (response) {
        case "a":
            Utils.currentStage(event).close();
            break;
        }
    }
}

CodePudding user response:

Your sendOptionToServer(...) method, and in particular your waitForResponse(...) method, contains blocking calls that block execution until they are complete (i.e. until you receive a response from the server). Since you're running these on the FX Application Thread, you prevent that thread from doing its normal work until those calls complete. This means it won't update the UI or process any user events until you have received and processed the response from the server.

You should place the calls to blocking methods in a background thread to allow the FX Application Thread to proceed in the meantime. The javafx.concurrent API makes this reasonably easy to do; here a Task should suffice.

Here's a version that uses a Task. I also used a "try with resources" to ensure everything that needs to be closed is correctly closed.

@FXML
private void sendOptionToServer(ActionEvent event) {
    
    Task<String> serverCommunicationTask = new Task<>() {
        @Override
        protected String call() throws Exception {
            try (
                Socket cliente = new Socket("localhost", 3322);
                PrintWriter pr = new PrintWriter(cliente.getOutputStream());
                BufferedReader bf = new BufferedReader(new InputStreamReader(cliente.getInputStream()));
            ) {
                pr.println(option);
                pr.flush();
                return bf.readLine();
            }
        }
    };

    serverCommunicationTask.setOnSucceeded(event -> {
        if ("a".equals(serverCommunicationTask.getValue())) {
            rootPane.getScene().getWindow().hide();
        }
    });

    serverCommunicationTask.setOnFailed(event -> {
        event.getException().printStackTrace();
        // handle exception...
    });

    Thread thread = new Thread(serverCommunicationTask);
    thread.setDaemon(true);
    thread.start();
        
}
  • Related