Tutorial JavaFX for Beginners

Discussion in 'Tutorials & Resources' started by Baddest Man on Earth, Jan 18, 2015.

  1. 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 bots with RuneMate before attempting to write a RuneMate script bot 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:

    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 bot class:

    Now when we run the script bot, 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.
    Serene, Defeat3d, Furor and 1 other person like this.
  2. 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.
  3. 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
  4. 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.
    Aidden likes this.
  5. Actually, most nubs don't know who to use layouts and tend to use a null layout in swing.
  6. 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
    #6 SlashnHax, Jan 22, 2015
    Last edited: Jan 22, 2015
  7. So how did you add it as a Resource?
  8. I added it as a resource in the manifest as shown in the official manifest tutorial/guide. I wasn't sure if you could use a relative reference as the path, but you can.
  9. Show your xml file please
  10. Code (Text):
    2. <manifest>
    3.     <main-class>com.slashnhax.scripts.taskscripts.haxagility.HaxAgility</main-class>
    4.     <name>HaxAgility</name>
    5.     <description>Runs Agility courses! Supports Burthorpe, Gnome Stronghold, Barbarian Outpost, Wilderness, Ape Atoll, Gnome Adv., Barbarian Adv. and Hefin </description>
    6.     <version>1.8.1</version>
    7.     <compatibility>
    8.         <game-type>RS3</game-type>
    9.     </compatibility>
    10.     <categories>
    11.         <category>AGILITY</category>
    12.     </categories>
    13.     <!--Required to publish on the bot store-->
    14.     <internal-id>HaxAgility</internal-id>
    15.     <!--The rest are optional-->
    16.     <resources>
    17.         <resource>com/slashnhax/scripts/taskscripts/haxagility/gui/HaxAgilityGUI.fxml</resource>
    18.     </resources>
    19. </manifest>
  11. Thank you @SlashnHax for that and thank you @Supreme Leader for the the wonderful tutorial.
  12. @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;

    #12 Geashaw, Feb 2, 2015
    Last edited: Feb 2, 2015
  13. You've got a comma between ResourceBundle and resources btw
  14. 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
  15. When presented with import options the right one (almost) never has awt in the package name and (almost) always has javafx in it.
  16. Thank you, are you willing to gimmeh a downloadable?
  17. 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
  18. The Oracle Tutorials are good for javafx.
  19. Where exactly do you put the setRock section of code? That's the only thing I'm having problems with at the moment :(
    awesome123man likes this.
  20. BTW for anybody wondering Oracle has stoped work on the scene builder so here is the new download Scene Builder. The location is in your appdata then scenebuilder! Hope this helped people!
    TheVTM likes this.

Share This Page