I am using Java and JavaFX for my UI, and I want to put an Image after some Label. The Image should be the same height as the Label.
However, if I bind the `fitHeightProperty` to the Labels height, the Image gets resized correctly, but the AnchorPane around it does not.
The expected behaviour would be, that the AnchorPane, too, shrinks; together with the Image.
Edit: The Problem seems to be the same as it is here: http://stackoverflow.com/questions/20610950/scrollpane-containing-imageview-does-not-update-its-scrollbars-after-calling-set, where it is stated that this behaviour is a bug. The AnchorPane does not seem to notice the changed size of the Image. Also I can confirm that updating the scene makes the AnchorPane snap to the desired size (I tried it with changing the label-text on MouseClick).
Also, the problem can be reproduced with fxml-only (without java-code).
I have created a code example to reproduce to supposed bug.
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application
{
public static void main(String[] args)
{
launch(args);
}
public void start(Stage stage) throws Exception
{
//Setup Window
stage.setTitle("ImageResizeDemo");
stage.setWidth(400);
stage.setHeight(400);
stage.setScene(new Scene(new Main().getUI()));
stage.show();
}
public Parent getUI()
{
AnchorPane main = new AnchorPane();
AnchorPane inner = new AnchorPane();
Label label = new Label();
label.setText("Test");
inner.getChildren().add(label);
//Load image and bind its height to the height of the label
ImageView image = new ImageView(new Image(this.getClass().getResourceAsStream("image.png")));
image.fitHeightProperty().bind(label.heightProperty());
inner.getChildren().add(image);
main.getChildren().add(inner);
//Add dotted borders around the panes to see the size of them
addDebugBorder(inner);
addDebugBorder(main);
return main;
}
public static void addDebugBorder(Region region)
{
region.borderProperty().set(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.DOTTED, CornerRadii.EMPTY, BorderWidths.DEFAULT)));
}
}
To run this, you only need an `image.png` file next to the class.
This already exists as a StackOverflow question: https://stackoverflow.com/questions/24256099/imageview-does-not-resize-properly
However, if I bind the `fitHeightProperty` to the Labels height, the Image gets resized correctly, but the AnchorPane around it does not.
The expected behaviour would be, that the AnchorPane, too, shrinks; together with the Image.
Edit: The Problem seems to be the same as it is here: http://stackoverflow.com/questions/20610950/scrollpane-containing-imageview-does-not-update-its-scrollbars-after-calling-set, where it is stated that this behaviour is a bug. The AnchorPane does not seem to notice the changed size of the Image. Also I can confirm that updating the scene makes the AnchorPane snap to the desired size (I tried it with changing the label-text on MouseClick).
Also, the problem can be reproduced with fxml-only (without java-code).
I have created a code example to reproduce to supposed bug.
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application
{
public static void main(String[] args)
{
launch(args);
}
public void start(Stage stage) throws Exception
{
//Setup Window
stage.setTitle("ImageResizeDemo");
stage.setWidth(400);
stage.setHeight(400);
stage.setScene(new Scene(new Main().getUI()));
stage.show();
}
public Parent getUI()
{
AnchorPane main = new AnchorPane();
AnchorPane inner = new AnchorPane();
Label label = new Label();
label.setText("Test");
inner.getChildren().add(label);
//Load image and bind its height to the height of the label
ImageView image = new ImageView(new Image(this.getClass().getResourceAsStream("image.png")));
image.fitHeightProperty().bind(label.heightProperty());
inner.getChildren().add(image);
main.getChildren().add(inner);
//Add dotted borders around the panes to see the size of them
addDebugBorder(inner);
addDebugBorder(main);
return main;
}
public static void addDebugBorder(Region region)
{
region.borderProperty().set(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.DOTTED, CornerRadii.EMPTY, BorderWidths.DEFAULT)));
}
}
To run this, you only need an `image.png` file next to the class.
This already exists as a StackOverflow question: https://stackoverflow.com/questions/24256099/imageview-does-not-resize-properly
- relates to
-
JDK-8091789 make ImageView resizable
- Open