A Label control will not display both its text and its graphic if the text is bound to a string property and is not updated before the Scene is shown. The example code below illustrates the issue. When using setText and setGraphic, everything works fine.
If the Label's textProperty is bound to a StringProperty AND a graphic is set, then the text will not be shown unless the StringProperty is initialized before the Scene is shown. If no graphic is set, then the text is updated properly regardless of when the update takes place.
If the following code is run as-is, then no text will show up on the label.
public class ProgressLabelTest extends Application {
private SimpleStringProperty message = new SimpleStringProperty();
@Override
public void start(Stage stage) throws Exception {
Rectangle r = new Rectangle(20, 20);
Label l = new Label();
// This shows both text and graphic - order doesn't matter.
// l.setText("Hello, world");
// l.setGraphic(r);
// This shows both text and graphic as long as the text is
// updated before the scene is shown
l.textProperty().bind(message);
l.setGraphic(r); // Comment this out and "Hello, world again" will show regardless of the next line
//message.set("Hello, world (bound)"); // Uncomment this and "Hello, world again" will show
Scene s = new Scene(new Group(l), 200, 200);
stage.setScene(s);
stage.show();
message.set("Hello, world again (bound)");
}
public static void main(String[] args) {
ProgressLabelTest.launch(args);
}
}
If the Label's textProperty is bound to a StringProperty AND a graphic is set, then the text will not be shown unless the StringProperty is initialized before the Scene is shown. If no graphic is set, then the text is updated properly regardless of when the update takes place.
If the following code is run as-is, then no text will show up on the label.
public class ProgressLabelTest extends Application {
private SimpleStringProperty message = new SimpleStringProperty();
@Override
public void start(Stage stage) throws Exception {
Rectangle r = new Rectangle(20, 20);
Label l = new Label();
// This shows both text and graphic - order doesn't matter.
// l.setText("Hello, world");
// l.setGraphic(r);
// This shows both text and graphic as long as the text is
// updated before the scene is shown
l.textProperty().bind(message);
l.setGraphic(r); // Comment this out and "Hello, world again" will show regardless of the next line
//message.set("Hello, world (bound)"); // Uncomment this and "Hello, world again" will show
Scene s = new Scene(new Group(l), 200, 200);
stage.setScene(s);
stage.show();
message.set("Hello, world again (bound)");
}
public static void main(String[] args) {
ProgressLabelTest.launch(args);
}
}
- duplicates
-
JDK-8128953 Setting empty string in Label.setText() hides future text
-
- Closed
-