简体   繁体   中英

How can I set the margin of a BorderPane in fxml?

I am trying to run this fxml code in javafx:

<BorderPane fx:controller="com.bryantmorrill.chat.main.Controller"
      xmlns:fx="http://javafx.com/fxml" >

<center>
    <ScrollPane BorderPane.margin="25, 25, 25, 25">
        <content>
            <TextArea fx:id="chatArea" minWidth="200" maxWidth="450"
                      prefWidth="450" minHeight="200" prefHeight="400"
                      maxHeight="400"/>
        </content>
    </ScrollPane>
</center>

<bottom>
    <FlowPane BorderPane.margin="25, 25, 25, 25">
        <TextField fx:id="inputArea" minWidth="200" maxWidth="450" prefWidth="450"/>
        <Button text="Send" onAction="#sendMessage" minWidth="200" maxWidth="450" prefWidth="450"/>
    </FlowPane>

</bottom>

However, it fails when I try to set the margin this way:

<ScrollPane BorderPane.margin="25, 25, 25, 25">

I have also tried these methods:

<ScrollPane BorderPane.margin="25 25 25 25">
<ScrollPane BorderPane.margin="25">

This is the exception I get with all of them:

java.lang.IllegalArgumentException: Unable to coerce 25, 25, 25, 25 to class javafx.geometry.Insets.

This is my first time using JavaFX and I couldn't find any good examples of this. Thanks for any help!

You need to add the margin as subelement of the child node of the BorderPane :

<center>
    <ScrollPane>
        <BorderPane.margin>
             <Insets bottom="25.0" left="25.0" right="25.0" top="25.0" />
        </BorderPane.margin>
        <content>
            <TextArea fx:id="chatArea" minWidth="200" maxWidth="450"
                      prefWidth="450" minHeight="200" prefHeight="400"
                      maxHeight="400"/>
        </content>
    </ScrollPane>
</center>
<bottom>
    <FlowPane>
        <BorderPane.margin>
             <Insets bottom="25.0" left="25.0" right="25.0" top="25.0" />
        </BorderPane.margin>
        <TextField fx:id="inputArea" minWidth="200" maxWidth="450" prefWidth="450"/>
        <Button text="Send" onAction="#sendMessage" minWidth="200" maxWidth="450" prefWidth="450"/>
    </FlowPane>
</bottom>

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