Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :)

like image 805
urviguglani Avatar asked May 10 '15 04:05

urviguglani


People also ask

How do you make an image clickable in JavaFX?

Lets start with scenebuilder, open the fxml file. Drag-and-drop ImageView from Scenebuilder library(right panel). Once added, select the ImageView and give it a fx:id "iView" in this case, then goto Code section and add a function name in OnMouseClicked field. I named my function "LoginUser".

How do I make a button in FXML?

You can create a Button by instantiating the javafx. scene. control. Button class of this package and, you can set text to the button using the setText() method.

How do I program a button in JavaFX?

You create a button control by creating an instance of the Button class. Here is a JavaFX Button instantiation example: Button button = new Button("My Label"); The text to be displayed on the button is passed as parameters to the Button constructor.


2 Answers

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>
like image 78
AlmasB Avatar answered Oct 12 '22 23:10

AlmasB


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

like image 28
Abdul Saleem Avatar answered Oct 12 '22 23:10

Abdul Saleem