- Joined
- Nov 26, 2014
- Messages
- 616
- Thread Author
- #1
Before reading any further
JavaFX is the latest GUI framework for Java and will soon replace Swing GUIs. This is a quick tutorial on how to get started with JavaFX.
Start off by downloading and installing SceneBuilder:
http://www.oracle.com/technetwork/java/javase/downloads/sb2download-2177776.html
In your project create a new file:
Name your file (make sure the file type is .fxml):
Open the file in SceneBuilder:
Double click AnchorPane to add a parent Container for all other components to be placed in:
For this tutorial, I'll be adding a Combobox to select what rock to mine, a Label, and a Button.
After adding all your components, add a fx:id for all your components that will require user input (so that means don't assign a fx:id to label):
I set my Combobox to "selectRock" and Button to "btnStart".
Now save your fxml file and you should have a simple JavaFX GUI. Unfortunately, everything else will be handcoded.
Let's create the class that will be responsible for showing the file:
And the main script class:
Now when we run the script, the GUI will show up, but the ComboBox will be empty and the button won't do anything. We need to add a Controller that will be responsible for filling the ComboBox and handling input. Remember the fx:id's we set in SceneBuilder? This is where they come into play.
Finally, we have to set the Controller in the FXMLLoader from the TutorialGui class:
End result:
Thanks for reading. Like this post if you found it helpful.
- You should have intermediate programming experience before attempting JavaFX
- I use Intellij IDEA as my IDE, if you are more familiar with another IDE, use that one, the focus is not on the IDE and a few Google searches should resolve any questions with different IDEs
- You should know the basics of writing scripts with RuneMate before attempting to write a RuneMate script using a JavaFX GUI
JavaFX is the latest GUI framework for Java and will soon replace Swing GUIs. This is a quick tutorial on how to get started with JavaFX.
Start off by downloading and installing SceneBuilder:
http://www.oracle.com/technetwork/java/javase/downloads/sb2download-2177776.html
In your project create a new file:
![TZOMsox.png](/community/proxy.php?image=http%3A%2F%2Fi.imgur.com%2FTZOMsox.png&hash=47b35f74144ea146f0a01b4f3dc7b043)
Name your file (make sure the file type is .fxml):
![H9861jD.png](/community/proxy.php?image=http%3A%2F%2Fi.imgur.com%2FH9861jD.png&hash=de675489d374a947f2ebc8d25b17f840)
Open the file in SceneBuilder:
![dtsmaUr.png](/community/proxy.php?image=http%3A%2F%2Fi.imgur.com%2FdtsmaUr.png&hash=7c7b89c3ffdeecd2fb40ec670b22e2a1)
Double click AnchorPane to add a parent Container for all other components to be placed in:
![Pd3VFNg.png](/community/proxy.php?image=http%3A%2F%2Fi.imgur.com%2FPd3VFNg.png&hash=bfdb329e5ddeba0fe692513cc7d840c5)
For this tutorial, I'll be adding a Combobox to select what rock to mine, a Label, and a Button.
After adding all your components, add a fx:id for all your components that will require user input (so that means don't assign a fx:id to label):
![oiAj0ve.png](/community/proxy.php?image=http%3A%2F%2Fi.imgur.com%2FoiAj0ve.png&hash=eeba91eb7cff42414176a9f60a0be890)
I set my Combobox to "selectRock" and Button to "btnStart".
Now save your fxml file and you should have a simple JavaFX GUI. Unfortunately, everything else will be handcoded.
Let's create the class that will be responsible for showing the file:
![WH4mend.png](/community/proxy.php?image=http%3A%2F%2Fi.imgur.com%2FWH4mend.png&hash=59c0cfba87028ea16d30477ce076fa3a)
And the main script class:
![UIMatoP.png](/community/proxy.php?image=http%3A%2F%2Fi.imgur.com%2FUIMatoP.png&hash=00b6baedd1ecb9011806ee0790280473)
Now when we run the script, the GUI will show up, but the ComboBox will be empty and the button won't do anything. We need to add a Controller that will be responsible for filling the ComboBox and handling input. Remember the fx:id's we set in SceneBuilder? This is where they come into play.
![xRyo22M.png](/community/proxy.php?image=http%3A%2F%2Fi.imgur.com%2FxRyo22M.png&hash=14bbab95b7662fd3180a2b98916a1127)
Finally, we have to set the Controller in the FXMLLoader from the TutorialGui class:
![4iCdkqa.png](/community/proxy.php?image=http%3A%2F%2Fi.imgur.com%2F4iCdkqa.png&hash=7e1ec27205b954a5ac30a62fad32b304)
End result:
![tejmQi6.png](/community/proxy.php?image=http%3A%2F%2Fi.imgur.com%2FtejmQi6.png&hash=8dd40033dc55b2eb1bbf3e2f130d2608)
Thanks for reading. Like this post if you found it helpful.