2

There are obviously the two methods getWidth and getHeight but they return the old label size if we just changed the Label text value.

For example, in this code, the background is not correctly resized:

Label speedLabel = new Label();
Rectangle backgroundLabel = new Rectangle();

// Some initialization

// Change the label text
speedLabel.setText(connection.getSpeed_bps()); // Sets a bigger number

// Adjust the background
backgroundLabel.setWidth(speedLabel.getWidth());
backgroundLabel.setHeight(speedLabel.getHeight());

After the initialization, my Label is like that:

enter image description here

And after the text change and the background adjustment, my Label is like that:

enter image description here

I had a look at this post but it recommends a deprecated method:

How to get label.getWidth() in javafx

Community
  • 1
  • 1
Jeankowkow
  • 814
  • 13
  • 33
  • You could bind the **backgroundLabel width & height** to the **speedLabel width & height** e.g. – SSchuette Jun 29 '16 at 10:29
  • 2
    Possible duplicate of [How to calculate the pixel width of a String in JavaFX?](http://stackoverflow.com/questions/13015698/how-to-calculate-the-pixel-width-of-a-string-in-javafx) (Also [Get the height of a node in JavaFX (generate a layout pass)](http://stackoverflow.com/questions/26152642/get-the-height-of-a-node-in-javafx-generate-a-layout-pass)) – fabian Jun 29 '16 at 10:37

2 Answers2

4

The reason for returning the "old" size that the label actually doesn't get updated on the GUI in the moment when you set the textProperty of it, therefore the size is not changed.

You can listen to the widthProperty and heightProperty of the Label and resize the Rectangle in the listener:

speedLabel.widthProperty().addListener((obs, oldVal, newVal) -> {
    backgroundLabel.setWidth(newVal.doubleValue());
});

speedLabel.heightProperty().addListener((obs, oldVal, newVal) -> {
    backgroundLabel.setHeight(newVal.doubleValue());
});

or simply use bindings between the properties:

backgroundLabel.heightProperty().bind(speedLabel.heightProperty());
backgroundLabel.widthProperty().bind(speedLabel.widthProperty());

But if you just want to achieve a Label with some background, you don't actually need a Rectangle, just some CSS - you can check this question: FXML StackPane doesn't align children correctly

Community
  • 1
  • 1
DVarga
  • 21,311
  • 6
  • 55
  • 60
  • "There is no setWidth method for a Label" -> Sorry, I forgot some code declarations. Your solution works good, thanks: `backgroundLabel.widthProperty().bind(speedLabel.widthProperty());` `backgroundLabel.heightProperty().bind(speedLabel.heightProperty());` But can I add a margin at the label width? – Jeankowkow Jun 29 '16 at 11:31
  • I was just assuming that the other control is a `Label` also because of its name. Now with the update I can see its a `Rectangle` -> answer is updated. About the margin: Now as your question is updated, I can see what you want to achieve. Please see the end of the updated answer. – DVarga Jun 29 '16 at 11:47
1

You can do it in two ways.

Approach 1: Give the background color of your label as that of rectangle. So that whatever is the Label's size, your background rectangle will take the width accordingly.

label.setStyle("-fx-background-color:grey; -fx-padding:5");

Approach 2: Bind the Rectangle's size according to your label size

rectangle.prefWidthProperty(label.widthProperty());
rectangle.prefHeightProperty(label.heightProperty());
Harshita Sethi
  • 2,035
  • 3
  • 24
  • 46