how to make image buttons on java fxml using scene builder?
Asked Answered
F

4

6

I am using netbeans and want to use media file from my desktop to replace the boring button.

So this is my code. I want it so the image becomes the button.

<Button layoutX="252.0" layoutY="177.0" mnemonicParsing="false" prefHeight="57.0" prefWidth="135.0" text="Button!" textFill="BLUE">
     <font>
        <Font name="Avenir Next Regular" size="13.0" />
     </font>
  </Button>

Thanks in advance :)

Fictile answered 10/5, 2015 at 4:8 Comment(1)
See this: #16340769Shackleford
P
13

In your fxml file, import the image package:

<?import javafx.scene.image.*?>

then just before the button, assuming image.png is located under "images/" directory and "images/" is located in the same directory as .fxml:

<fx:define>
   <Image fx:id="btnImage" url="images/image.png" />
</fx:define>

Then just add the following to your button definition

<Button layoutX="252.0" layoutY="177.0" mnemonicParsing="false" prefHeight="57.0" prefWidth="135.0" text="Button!" textFill="BLUE">
     <font>
        <Font name="Avenir Next Regular" size="13.0" />
     </font>

     <graphic>
        <ImageView image="$btnImage" />
     </graphic>
  </Button>
Poock answered 10/5, 2015 at 9:10 Comment(0)
A
16

The question asks for how to add using scene builder. Here's how..

Screenshot

Drag imageview from the controls and drop it on top of a button. Note the hierarchy. It should go inside the button. Then you can adjust the size, source and other things within the inspector.

I got this as result

Got this as result within scene builder

Angle answered 2/6, 2019 at 12:10 Comment(0)
P
13

In your fxml file, import the image package:

<?import javafx.scene.image.*?>

then just before the button, assuming image.png is located under "images/" directory and "images/" is located in the same directory as .fxml:

<fx:define>
   <Image fx:id="btnImage" url="images/image.png" />
</fx:define>

Then just add the following to your button definition

<Button layoutX="252.0" layoutY="177.0" mnemonicParsing="false" prefHeight="57.0" prefWidth="135.0" text="Button!" textFill="BLUE">
     <font>
        <Font name="Avenir Next Regular" size="13.0" />
     </font>

     <graphic>
        <ImageView image="$btnImage" />
     </graphic>
  </Button>
Poock answered 10/5, 2015 at 9:10 Comment(0)
B
2

Above answer is partially correct , without specifying the '@' and '$' symbol in url and in "ImageView" like

<Image fx:id="playbutImg" url="@image/image.png">
<ImageView image="$playbutImg" >

@ symbol is used for diffrentiate between string and relative directory , if we pass without the @ symbol it will take as a string rather than the relative directory.

Burgas answered 2/1, 2017 at 5:0 Comment(0)
D
-1
    <fx:define>
            <Image fx:id="NewTracker" url="@resources/NewTracker.bmp"/>
        </fx:define>
        <Button layoutX="100.0" layoutY="100.0" mnemonicParsing="false" text="New Tracker" >
                <graphic>
                   <ImageView image="$NewTracker"/>
                </graphic>
        </Button>
Dundalk answered 12/2, 2018 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.