简体   繁体   中英

How to fix or set the label height and width with respect to GridPane row height and column width by using JavaFX and Scene Builder?

How to set height and width of the Label with respect to GridPane row height and column width?

Please check my below images

this image is minimized screen 这是我最小化的屏幕图像

this one is maximized screen 这是我最大化的屏幕图像

this image is my label layout properties
这是我的标签布局属性

I want to resize the Label with respect to GridPane row height and column width.

Through code I am setting the background color for the label like below.

if(cmnd1.WIM.equals("Y")){
   javafx.application.Platform.runLater( new Runnable() {
        @Override
        public void run() {
            lsduController1.oneWIM.setGraphic(new ImageView(new Image("/images/Wim_T.png")));
            lsduController1.oneWIM.setStyle("-fx-background-color: none;");
        }
   });
} else {
    javafx.application.Platform.runLater( new Runnable() {
        @Override
        public void run() {
            lsduController1.oneWIM.setGraphic(new ImageView(new Image("/images/Wim_F.png")));
            lsduController1.oneWIM.setStyle("-fx-background-color: Red;");
        }
    });
}

The issue here is setting the maxHeight and maxSize to USE_COMPUTED_SIZE .

Use MAX_VALUE instead

<Label style="-fx-background-color: red;" text="Label">
    <maxHeight>
        <Double fx:constant="MAX_VALUE"/>
    </maxHeight>
    <maxWidth>
        <Double fx:constant="MAX_VALUE"/>
    </maxWidth>
</Label>

(This should be available from the dropdown in SceneBuilder too.)

To resize the ImageView too, bind the fitWidth and fitHeight properties to the label size. Eg:

ImageView imageView = new ImageView(new Image("/images/Wim_F.png"));
imageView.setPreserveRatio(true);
imageView.fitWidthProperty().bind(lsduController1.oneWIM.widthProperty().subtract(10d));
imageView.fitHeightProperty().bind(lsduController1.oneWIM.heightProperty().subtract(10d));
lsduController1.oneWIM.setGraphic(imageView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM