Edu Castrillon
Edu Castrillon

Reputation: 587

Inclusion of tags in FXML

I would like to know if there is a way to include pieces of code (tags) defined in the same FXML file. What I want is to include the same image several times without having to copy-paste all properties :

<HBox>
  <Label translateY="25" alignment="center"  text="">
    <graphic>
      <ImageView fitWidth="100" preserveRatio="true" smooth="true">
        <image>
          <Image url="arrow_right.png"/>
        </image>
      </ImageView>
    </graphic>
  </Label>
</HBox>

I would like something like :

<fx:define>
  <Label fx:id="myLabel" translateY="25" alignment="center"  text="">
    <graphic>
      <ImageView fitWidth="100" preserveRatio="true" smooth="true">
        <image>
          <Image url="arrow_right.png"/>
        </image>
      </ImageView>
    </graphic>
  </Label>
</fx:define>

<HBox>
    <fx:include source="$myLabel" />
</HBox>

I've read this answer https://stackoverflow.com/a/8490764/1606953, but it covers inclusion of external sources (=other files). I'm more interested in just including small pieces of code

Thank you all

Upvotes: 1

Views: 3137

Answers (2)

Edu Castrillon
Edu Castrillon

Reputation: 587

I (almost) found what I was looking for :

my-tab.fxml :

<HBox>
  <fx:define>
    <Label fx:id="arrow" alignment="center"  text="" maxWidth="Infinity" VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS">
     <graphic>
        <ImageView fitWidth="100" preserveRatio="true" smooth="true">
           <image>
             <Image url="peet/resources/images/arrow_right.png"/>
           </image>
        </ImageView>
     </graphic>
    </Label>
  </fx:define>
  <children>
    <Button wrapText="true" text="AAA" maxHeight="Infinity" maxWidth="Infinity"  VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS" /> 
    <fx:reference source="arrow" />                              
    <Button text="BBB" wrapText="true" minHeight="150" minWidth="100" maxWidth="Infinity" VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS" /> 
    <fx:reference source="arrow" />
    <Button text="CCC" wrapText="true" minHeight="150" minWidth="100" maxWidth="Infinity" VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS" /> 
    <fx:reference source="arrow" />
    <Button  text="DDD" wrapText="true" minHeight="150" minWidth="100" maxWidth="Infinity" VBox.vgrow="ALWAYS" HBox.hgrow="ALWAYS" /> 
  </children>       
</HBox> 

This only works if we only make 1 reference. If we make 3 references, like up here, we get this exception :

Children: duplicate children added: parent = HBox@2df02fde
file:/C:/.../my-tab.fxml:48
file:/C:/.../Xxx.jar!/xxx/client/MainTabs.fxml:16
  at javafx.scene.Parent$1.onProposedChange(Parent.java:307)
  at com.sun.javafx.collections.VetoableObservableList.add(VetoableObservableList.java:165)
  at com.sun.javafx.collections.ObservableListWrapper.add(ObservableListWrapper.java:144)
  at javafx.fxml.FXMLLoader$Element.add(FXMLLoader.java:130)
  at javafx.fxml.FXMLLoader$PropertyElement.add(FXMLLoader.java:1179)
  at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:609)
  at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2430)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2136)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
  at javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:937)
  at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:567)
  at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2314)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2131)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2742)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2694)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2683)
  at xxx.client.XXX.start(XXX.java:21)
  at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
  at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)
  at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
  at com.sun.glass.ui.win.WinApplication$2$1.run(WinApplication.java:67)
  at java.lang.Thread.run(Thread.java:722)  

Any options ?

Upvotes: 0

Greg Brown
Greg Brown

Reputation: 111

<fx:include> is definitely the best way to handle this. You can't use <fx:define> here since "myLabel" can only ever have a single parent node. By placing the Label in an include, you'll be able to create multiple instances of the content, each of which can have its own parent.

Upvotes: 1

Related Questions