简体   繁体   中英

JavaFX: Load scene from FXML but add label from code

I have the following FXML that I load at a button press in another scene

<GridPane fx:id="gridPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" styleClass="root" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="VMProvOpenstack.Controller">
    <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="295.0" minWidth="10.0" prefWidth="162.0" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="295.0" minWidth="10.0" prefWidth="162.0" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="358.0" minWidth="0.0" prefWidth="358.0" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="450.0" minWidth="0.0" prefWidth="0.0" />
    </columnConstraints>
    <rowConstraints>
        <RowConstraints maxHeight="166.0" minHeight="10.0" prefHeight="96.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="238.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="305.0" minHeight="10.0" prefHeight="257.0" vgrow="SOMETIMES" />
    </rowConstraints>
    <children>
        <Region prefHeight="36.0" prefWidth="538.0" style="-fx-background-color: brown;" GridPane.rowIndex="1">
            <effect>
                <Blend />
            </effect>
        </Region>
        <Region prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: brown;" GridPane.columnIndex="1" GridPane.rowIndex="1" />
        <Region layoutX="10.0" layoutY="112.0" prefHeight="36.0" prefWidth="538.0" style="-fx-background-color: brown;" GridPane.columnIndex="2" GridPane.rowIndex="1">
            <effect>
                <Blend />
            </effect>
        </Region>
        <Label fx:id="loggedinUser" contentDisplay="RIGHT" prefHeight="21.0" prefWidth="199.0" text="Loged in as:  " textFill="green" GridPane.columnIndex="2" GridPane.rowIndex="0" />
        <Label fx:id="ipopenstack" contentDisplay="RIGHT" prefHeight="21.0" prefWidth="199.0" text="IP : 192.168.131.132" textFill="#eb0f0f" translateX="-40.0" GridPane.columnIndex="2" GridPane.rowIndex="2" />
    </children>
</GridPane>

the code is:

@FXM Label ipopenstack=new Label();
@FXML GridPane gridPane=new GridPane();
//...
Stage stageTheEventSourceNodeBelongs = (Stage)((Node)event.getSource()).getScene().getWindow();
try {
    Scene scene= new Scene(FXMLLoader.load(getClass().getResource("my.fxml")),700,500);
    stageTheEventSourceNodeBelongs.setScene(scene);
} catch (IOException e) {
    e.printStackTrace();
}

if i do this the everything is displayed just fine. But i want to add a Label with dynamic message to this scene and still keep everything from the fxml. If i do this , where message is a String

Label ipL=new Label(message);                  
gridPane.add(ipL, 2, 2);
scene.setRoot(gridPane);

before setScene , it will add my label but not the elements from fxml.

Any suggestions, please? Thanks

Never initialize @FXML -injected fields. You should have

@FXML Label ipopenstack ;
@FXML GridPane gridPane ;

instead of

@FXML Label ipopenstack=new Label();
@FXML GridPane gridPane=new GridPane();

try first to get the pane from scene and then update

Parent p=scene.getRoot();
// Navigate to the appropriate pane object
//the try adding the label to the pane object
pane.add(ipL, 2, 2);

and make sure all this is done within a JavaFx thread(worker thread)

Or one more thing that you can do is keep the label added and hidden now when you need to update the message just update the Label with the message and turn off its hiding

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