JavaFX线程使UI无响应,任何替代[线程延迟]



JavaFX线程使UI变慢并挂起。

这是代码,如果有人能建议原因或替代方法,将会很有帮助。

这是一个MVC应用。当代码碰到这个线程时,它工作正常,UI不会挂起,但它不是,然后它变成空白和无响应。

这是一个线程延迟问题。

Platform.runLater(new Runnable() {
    @Override
    public void run() {
        DecimalFormat df = new DecimalFormat("#,##0.00");
        setLabel(bidSide, viewResource_.getString(SELL), 95, 121, JFXConstants.jfxWhiteSmoke);
        setLabel(askSide, viewResource_.getString(BUY), 145, 121, JFXConstants.jfxWhiteSmoke);
        setLabel(bidAmount, df.format(0.0), 0, 122, JFXConstants.jfxGrey);
        setLabel(askAmount, df.format(0.0), 146, 122, JFXConstants.jfxGrey);
        bidAmount.setPrefWidth(110);
        bidAmount.setScaleX(0.87);
        bidAmount.setScaleY(0.87);
        bidAmount.setAlignment(Pos.CENTER_LEFT);
        askAmount.setPrefWidth(110);
        askAmount.setScaleX(0.87);
        askAmount.setScaleY(0.87);
        askAmount.setAlignment(Pos.CENTER_RIGHT);
        unlock.setVisible(false);
        lock.setVisible(false);
        lock.setOpacity(0.7);
        root.setStyle(FX_BACKGROUND_COLOR  + fxBlackString);
        greyPanel = createRectangle(310.0, 135.0, JFXConstants.jfxGrey, 0.0, 5.0);
        greyPanel.setOpacity(0.6);
        root.getChildren().add(greyPanel);
        if (getFastFXController().getModel() != null && getFastFXController().getModel()
                .getCurrency_() != null &&
                !getFastFXController().getModel().getCurrency_().equals(LNFConstants.EMPTY_STRING)) {
            try {
                javafx.scene.image.Image image =
                        new javafx.scene.image.Image(setImagePath(getFastFXController().getModel()
                                .getCurrency_()));
                flagView = new ImageView(image);
            } catch (Exception e) {
                flagView = new ImageView();
            }
            flagView.setLayoutX(6.0);
            flagView.setLayoutY(11.0);
            flagView.setFitWidth(242);
            flagView.setFitHeight(108);
            root.getChildren().add(flagView);
        }
        bidRect = new javafx.scene.shape.Rectangle();
        offerRect = new javafx.scene.shape.Rectangle();
        nearTenorGroup.getChildren().addAll(nearTenorRect,
                createJFxLabel(nearLabel, NEAR, 1, 0, JFXConstants.jfxWhite, javafx.scene.text.Font.font(FONT_ARIAL, FontWeight.NORMAL, 12)),
                createJFxLabel(tenor_, FastFXView.this.getFastFXController().getModel().getTenor_(), 9, 7, JFXConstants.jfxWhite, javafx.scene.text.Font.font(FONT_ARIAL, FontWeight.EXTRA_BOLD, 14))
        );
        nearLabel.setOpacity(0.4);
        tenor_.setOnMouseClicked(new EventHandler<javafx.scene.input.MouseEvent>() {
            @Override
            public void handle(javafx.scene.input.MouseEvent mouseEvent) {
                popUpGroups.get(NEAR_TENOR).requestFocus();
                popUpGroups.get(NEAR_TENOR).setVisible(true);
                popUpGroups.get(NEAR_TENOR).setLayoutX(219);
                popUpGroups.get(NEAR_TENOR).setLayoutY(1);
                popUpGroups.get(NEAR_TENOR).requestFocus();
                tenor_.setTextFill(JFXConstants.jfxGrey);
            }
        });
        tenor_.setOnMouseEntered(new EventHandler<javafx.scene.input.MouseEvent>() {
            @Override
            public void handle(javafx.scene.input.MouseEvent mouseEvent) {
                tenor_.setOpacity(0.8);
            }
        });
        tenor_.setOnMouseExited(new EventHandler<javafx.scene.input.MouseEvent>() {
            @Override
            public void handle(javafx.scene.input.MouseEvent mouseEvent) {
                tenor_.setOpacity(1);
            }
        });
        root.getChildren().add(nearTenorGroup);
        farTenorGroup.getChildren().addAll(farTenorRect,
                createJFxLabel(farLabel, FAR, 1, 0, JFXConstants.jfxWhite, javafx.scene.text.Font.font(FONT_ARIAL, FontWeight.NORMAL, 12)),
                createJFxLabel(tenorFar_, FastFXView.this.getFastFXController().getModel().getTenorFar_(), 9, 7, JFXConstants.jfxWhite, javafx.scene.text.Font.font(FONT_ARIAL, FontWeight.EXTRA_BOLD, 14))
        );
        farLabel.setOpacity(0.4);
        tenorFar_.setOnMouseClicked(new EventHandler<javafx.scene.input.MouseEvent>() {
            @Override
            public void handle(javafx.scene.input.MouseEvent mouseEvent) {
                popUpGroups.get(FAR_TENOR).requestFocus();
                popUpGroups.get(FAR_TENOR).setVisible(true);
                popUpGroups.get(FAR_TENOR).setLayoutX(219);
                popUpGroups.get(FAR_TENOR).setLayoutY(5);
                popUpGroups.get(FAR_TENOR).requestFocus();
                tenorFar_.setTextFill(JFXConstants.jfxGrey);
            }
        });
        tenorFar_.setOnMouseEntered(new EventHandler<javafx.scene.input.MouseEvent>() {
            @Override
            public void handle(javafx.scene.input.MouseEvent mouseEvent) {
                tenorFar_.setOpacity(0.8);
            }
        });
        tenorFar_.setOnMouseExited(new EventHandler<javafx.scene.input.MouseEvent>() {
            @Override
            public void handle(javafx.scene.input.MouseEvent mouseEvent) {
                tenorFar_.setOpacity(1);
            }
        });
        root.getChildren().add(farTenorGroup);
        nearTenorGroup.setLayoutX(257);
        nearTenorGroup.setLayoutY(10);
        farTenorGroup.setLayoutX(257);
        farTenorGroup.setLayoutY(25);
        setItemsForPopup(NEAR_TENOR, getFastFXController().getModel().getTenors_());
        setItemsForPopup(FAR_TENOR, getFastFXController().getModel().getTenorsFar_());
        mapRectToPane(bidRect, liveBid);
        mapRectToPane(offerRect, liveAsk);
        bidRect.setFill(JFXConstants.jfxTransparent);
        offerRect.setFill(JFXConstants.jfxTransparent);
        root.getChildren().addAll(liveBid, liveAsk);
        root.getChildren().addAll(flip, toggle, lock, unlock);
        root.getChildren().addAll(offerRect, bidRect);
        root.getChildren().addAll(bidSide, bidAmount, askSide, askAmount);
        mouseEvents();
    }
}

一句话:在后台的另一个线程中运行一些东西:使用Task。就像

    Task loadDatesFromDB = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            List<Anfrage> list = ...something works slow...
            Platform.runLater(() -> {                   
                progressImage.setImage(null);
            }); 
            return null;
        }            
    };
    new Thread(loadDatesFromDB).start();  

请记住,您可以从Platform.runLater中的另一个线程"工作"主线程(所有UI运行的地方)

你一定要读它:JavaFX中的并发性

相关内容

  • 没有找到相关文章

最新更新