你如何强迫Javafx立即更新宽度属性?

时间:2017-11-12 17:28:54

标签: java multithreading javafx

我遇到问题,我需要javafx在更改Label中的文字后立即更新widthProperty Label。它在更改文本后更改后显示,它会使应用程序再次更新Label的宽度。

具体来说,我有一个自定义颜色选择器对象,其Label颜色十六进制值位于Circle之上,两者背后隐藏着ColorPicker

我想在颜色选择器的值更改时更新此对象。但问题是,当我尝试重新缩放Label时,会使用旧的width属性导致它无法正确缩放:

   colorPicker.valueProperty().addListener((Observable e) ->{
        circle.setFill(colorPicker.getValue());
        colorText.setText(colorPicker.getValue().toString());
        colorText.setTextFill(colorPicker.getValue().invert());

        //The values are set before the function call, but the width isn't updated
        scaleText();
    });

private void scaleText(){
    colorText.applyCss(); //I was hoping this line would force an update, but no dice
    if(circle.getRadius() == 0d || colorText.getWidth() == 0d){
        return;
    }
    //The following line of code will use the old width property
    double scale = (circle.getRadius()*1.60)/colorText.getWidth();
    colorText.setScaleX(scale);
    colorText.setScaleY(scale);
}

我知道这是没有立即更新的价值,因为我能够修复'从另一个线程延迟后运行scaleText()的问题:

new Thread(() -> {
            try {
                Thread.sleep(25);
            } catch (InterruptedException ex) {
                Logger.getLogger(ColorPickerCircle.class.getName()).log(Level.SEVERE, null, ex);
            }
            scaleText();
        }).start();

但是,我想找到一个不依赖于延迟来自另一个线程的函数调用的解决方案。所以我的问题归结为:有没有办法强制立即widthProperty更新?

0 个答案:

没有答案
相关问题