I'm having an issue where I need javafx to update a Label's widthProperty immediately after changing the text in that Label. It appears after changing after changing text it takes the application an additional cycle to update the width of that Label.
Specifically I have a custom color picker object, which has Label with the color hex value laid on top of a Circle, with a hidden ColorPicker behind both of them.
I want to update this object when the value of the color picker changes. The problem however is that when I try to rescale the Label, the old width property is used causing it to scale incorrectly:
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);
}
I know that this is the value not being updated immediately because I was able to 'fix' the issue by running scaleText() after a delay from another thread:
new Thread(() -> {
try {
Thread.sleep(25);
} catch (InterruptedException ex) {
Logger.getLogger(ColorPickerCircle.class.getName()).log(Level.SEVERE, null, ex);
}
scaleText();
}).start();
However I would like to find a solution that doesn't rely on delaying the function call from another thread. So my question boils down to: Is there any way to force an immediate widthProperty update?