Let’s say that you want to display multiple buttons and have different actions take place because of them. Buttons in JavaFX are just as important as in other UI toolkits. Here is a tutorial giving an example of two JavaFX buttons changing the text of a JavaFX TextField.
If you are brand new to JavaFX, be sure to go through the JavaFX tutorial I wrote a while back. This tutorial assumes you are using Eclipse.
Create a JavaFX Project
The first step to creating our example is creating a JavaFX Project to work in. You can create your JavaFX Project from the menu by selecting the following.
File -> New -> Other …
Then select JavaFX Project then click Next>. Give your project a name, like ‘two_buttons’ and click Finish.
Create a Java class called ButtonsController in the same package as your Main class. Not too surprisingly, this will be your JavaFX controller for this little Button example.
Create a FXML file in the same package and call it ‘ButtonsPane.fxml’. You will find the template for it in File -> New -> Other … just as the JavaFX Project was there.
Configuring the JavaFX Scene
Open the FXML file, ButtonsPane.fxml, with SceneBuilder. The default root when your create your FXML file is AnchorPane. You will find your AnchorPane all by itself in the Hierarchy on the left hand side.
Select your AnchorPane. Open up the layout on the right hand side of SceneBuilder to see the Sizes configured for the AnchorPane. The default ones stink. Change Pref Width and Pref Height to a real number like 600 and 400.
Now you can actually see the AnchorPane in the central SceneBuilder window.
Drag a couple of Buttons and a TextField from the right hand Controller and place them in the AnchorPane. Double click on the each button and change the text on one Button to read, “Hello”, and on the other Button to read, “Bye Bye”
Select the Bye Bye Button and look in the Code section on the right. Change fx:id to read “byeBtn” and the On Action field to read, “sayGoodBye”. Do the same with the Hello Button, except make the values “helloBtn” in the fx:id field and “sayHello” in the On Action field.
Now for the TextField, select the TextField and set the fx:id to “sayItTF” in its Code area on the right.
Don’t forget to set your controller for the FXML! This is sooo easy to forget, but nothing in your JavaFX project will work if you forget to hook up your controller.
On the left hand side of the SceneBuilder, select Controller and set the value in the Controller class field to “application.ButtonsController”.
Save the work you have completed in SceneBuilder. It saves automatically to the ButtonsPane.fxml you opened from your Eclipse project. If you open up your FXML file after saving, it will look something like the following.
Grab a skeleton copy of the your controller code by going to View -> Show Sample Controller Skeleton. Remember to select the Comments checkbox, but not the Full checkbox.
Controlling Your JavaFX Button Behavior
The controller will once again just do some basic work. We’ll look at doing more complex work with your controller in the future.
Set your sayGoodBye() method to change the text in the TextField to “Good-bye!”, and the sayHello() method to set your text to “Hello there!”.
Your controller code will look similar to the following.
See, there really isn’t that much to it.
Main JavaFX Class
Finally, let’s set up the Main class for the JavaFX example and run it. The code for the Main class looks very similar to the code from the Hello World tutorial. You’ll find that happens a lot.
Here is the last bit of code.
And there you have it, a JavaFX app with two Buttons manipulating one TextField.