Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Tutorial JavaFX for Beginners

Joined
Nov 26, 2014
Messages
616
Before reading any further
  • 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


Name your file (make sure the file type is .fxml):
H9861jD.png


Open the file in SceneBuilder:
dtsmaUr.png


Double click AnchorPane to add a parent Container for all other components to be placed in:
Pd3VFNg.png


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


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


And the main script class:
UIMatoP.png


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


Finally, we have to set the Controller in the FXMLLoader from the TutorialGui class:
4iCdkqa.png


End result:
tejmQi6.png


Thanks for reading. Like this post if you found it helpful.
 
Mod Automation
Joined
Jul 26, 2013
Messages
3,044
It's a good start. I would reconsider the decision to use AnchorPanes. I have never found a use for them that can't be done more elegantly using a proper layout style. AnchorPane is the equivalent of using absolute locations in HTML; it's a dirty hack.
 
Author of MaxiBots
Joined
Dec 3, 2013
Messages
6,770
It's a good start. I would reconsider the decision to use AnchorPanes. I have never found a use for them that can't be done more elegantly using a proper layout style. AnchorPane is the equivalent of using absolute locations in HTML; it's a dirty hack.
Then give him a suggestion of what would be better to use, considering you were supposed to write us a jfx tutorial you nub! @Arbiter
 
Mod Automation
Joined
Jul 26, 2013
Messages
3,044
Then give him a suggestion of what would be better to use, considering you were supposed to write us a jfx tutorial you nub! @Arbiter
Which layout you should use depends on how you want it laid out. Personally, I find myself using GridPanes, VBoxes, and HBoxes often, but they all have their place.
 
Joined
Dec 10, 2014
Messages
3,255
Actually, most nubs don't know who to use layouts and tend to use a null layout in swing.
The default (or null) layout is BorderLayout iirc

Also, thanks for the tutorial. Showed me everything I needed to know to implement javafx, except for adding the .fxml file as a resource but I figured that out :D
 
Last edited:
The Pip Collector
Joined
Sep 14, 2014
Messages
445
The default (or null) layout is BorderLayout iirc

Also, thanks for the tutorial. Showed me everything I needed to know to implement javafx, except for adding the .fxml file as a resource but I figured that out :D
So how did you add it as a Resource?
 
Joined
Dec 10, 2014
Messages
3,255
Show your xml file please
Code:
<manifest>
    <main-class>com.slashnhax.scripts.taskscripts.haxagility.HaxAgility</main-class>
    <name>HaxAgility</name>
    <description>Runs Agility courses! Supports Burthorpe, Gnome Stronghold, Barbarian Outpost, Wilderness, Ape Atoll, Gnome Adv., Barbarian Adv. and Hefin </description>
    <version>1.8.1</version>
    <compatibility>
        <game-type>RS3</game-type>
    </compatibility>
    <categories>
        <category>AGILITY</category>
    </categories>
    <!--Required to publish on the bot store-->
    <internal-id>HaxAgility</internal-id>
    <!--The rest are optional-->
    <resources>
        <resource>com/slashnhax/scripts/taskscripts/haxagility/gui/HaxAgilityGUI.fxml</resource>
    </resources>
</manifest>
 
The Pip Collector
Joined
Sep 14, 2014
Messages
445
Code:
<manifest>
    <main-class>com.slashnhax.scripts.taskscripts.haxagility.HaxAgility</main-class>
    <name>HaxAgility</name>
    <description>Runs Agility courses! Supports Burthorpe, Gnome Stronghold, Barbarian Outpost, Wilderness, Ape Atoll, Gnome Adv., Barbarian Adv. and Hefin </description>
    <version>1.8.1</version>
    <compatibility>
        <game-type>RS3</game-type>
    </compatibility>
    <categories>
        <category>AGILITY</category>
    </categories>
    <!--Required to publish on the bot store-->
    <internal-id>HaxAgility</internal-id>
    <!--The rest are optional-->
    <resources>
        <resource>com/slashnhax/scripts/taskscripts/haxagility/gui/HaxAgilityGUI.fxml</resource>
    </resources>
</manifest>
Thank you @SlashnHax for that and thank you @Supreme Leader for the the wonderful tutorial.
 
Joined
Jan 8, 2015
Messages
1,427
@Supreme Leader Could you perhaps make this a downloadable zip/tar?
I'm a bit confused as to which files are which and where to place them.

Seeing these give me errors;

e9cceca80e.png
 
Last edited:
Joined
Nov 26, 2014
Messages
616
@Supreme Leader Could you perhaps make this a downloadable zip/tar?
I'm a bit confused as to which files are which and where to place them.

Seeing these give me errors;

e9cceca80e.png
What slash said and make sure you are importing the right ActionEvent class.

The right one being javafx.event.ActionEvent and not java.awt.event.ActionEvent
 
Mod Automation
Joined
Jul 26, 2013
Messages
3,044
When presented with import options the right one (almost) never has awt in the package name and (almost) always has javafx in it.
 
Joined
Jun 21, 2014
Messages
350
I'm confused as to how the manifest loads resources, what perspective is the resource supposed to be looked at from? I have a file in the actual folder D:\Work\Programming\Furor\build\classes\furor\DevinationFold\DeviGUI.fxml, but the actual project starts with Furor and I use furor for the top of the manifest.

Also, where do I load the
getResourceAsStream();
from?
 
Top