0

recently,I set out to do project with javafX,I'm a freshman.I need popupWindow and HTTP communication.Before the communication,I want to popup a window to alert for waiting.Hide the window When communication is end.But it can not popup the window ahead,who can help me.Sorry for my poor English.

This is the handler when I enter the textField.

  txtinput.addEventHandler(KeyEvent.KEY_PRESSED, (KeyEvent event) -> {
     if (event.getCode() == KeyCode.ENTER) {
        Platform.runLater(() -> {
            //Popup The window
            popupTransparentWindow.alertWindow(AlertWindowEnum.TYPE_NONE.getControllerClass(),
                    ShowConstants.ORDER_WAITING_FOR_RESPONSE, ShowConstants.TIP, ShowConstants.COMFIRM);
            //HTTP Communication
            OrderConfirmResponse orderConfirmResponse = confirmOrderService.confirmOrderInfo(posWindowController.getOrderList());
            //Hide the popupWindow
            popupTransparentWindow.close();
            if (orderConfirmResponse.isSuccess()) {
                TradeConstants.billNumber = orderConfirmResponse.getData().getTradeNumber();
            } else {
                popupTransparentWindow.alertWindow(AlertWindowEnum.TYPE_BUTTON.getControllerClass(),
                        orderConfirmResponse.getMessage(), ShowConstants.TIP, ShowConstants.COMFIRM);
            }
        });
    }
  });
  public void alertWindow(Class<?> t, String title, String tip, String btn) {
        AnchorPane paneWindow = new AnchorPane();
        ObservableList<Stage> stage = FXRobotHelper.getStages();
        paneWindow.setPrefWidth(1024);
        paneWindow.setMinHeight(220);
        //TODO 正式
        Parent parent = paneWindow;
        previousScene = PosApplication.homePage.getScene();
        pop = new Popup();
        pop.getContent().add(paneWindow);
        pop.setAutoFix(true);
        pop.setY(250);
        pop.centerOnScreen();
        pop.show(previousScene.getWindow());
        //alertMethod(t, title, tip, btn);
    }

The http response need about five seconds,but window can't popup ahead.if I hide the code

//popupTransparentWindow.close();

The window will be popup with the Http communication response together. Thanks

  • I can't find an exact duplicate, but there are other related questions here on SO. Your basic issue is that you are calling the long-running task (the HTTP call) on the UI thread. You should run it on a separate thread, and only call the methods updating UI inside `Platform.runLater`. Depending on the type of `confirmOrderService` it may even provide such functionality. See also https://stackoverflow.com/questions/13784333/platform-runlater-and-task-in-javafx or https://stackoverflow.com/questions/24985985/why-is-my-thread-making-my-javafx-application-lag or similar questions. – Itai Jan 30 '18 at 07:08
  • use javafx.concurrent.Task https://gist.github.com/sh9va/768049585e2eb8dbca487ebf9c5def84 – Shiva Kumar Jan 30 '18 at 07:12
  • BTW: it's unnecessary to use `Platform.runLater` from a event handler of a javafx node *directly*, since those event handlers are executed on the JavaFX application thread. – fabian Jan 30 '18 at 10:25
  • Thank you very much.I have solved my question When I put the HTTP CALL in the Task as the example. Perfect. I can go on my program. – Diggorykong Jan 31 '18 at 00:50

0 Answers0