Fast & Dirty Java GUI building on 2017

Java Gui of Choice (2017)

So first of all, yes, Javafx is still a thing.

You want to build a gui for your Java application?
You don't have many options, AWT, SWT and Swing are dead, JavaFX is the official way to go for a Java developer.

Yes, you can have an HTML gui for your java application, and that is a perfectly valid choice, which should be accomplished by using Vaadin or Apache Wicket, but those options force you to deploy your gui with a web server which could be uncomfortable for many a developer.

So JavaFX it is; Now, you can go around the web and learn how to use all of those plugins and wizards which will hide the simple mechanism of building a javafx gui or you can continue reading this post  :-)


JavaFX Installation/Configuration

The first thing you will want to do is to download JavaFX SceneBuilder, the SceneBuilder is Oracle's official app for creating nice FXML files using simple drag and drop techniques.
Although Oracle has open sourced this project and are actively maintaining it, they don't release the binaries any more as they want each IDE developer to implement the SceneBuilder into it's core code.

So, we depend on 3rd party entities to compile and build it for us:
Download it from here

Now that you have installed the Scenebuilder, all you need is just to integrate it into your IDE of choice (Eclipse in my case), it will enable you to right click on a FXML file then open it with scen builder from the IDE.

So, download/install/configure the best eclipse plugin for the job which currently is: e(fx)clipse
Go to your project properties and add the javafx SDK library to the project.

The above is all you need inallation/configuration wise.


Coding with JavaFX

You will want to keep MVC design, which means that you should go on and create packages for Model, View and Controllers.

Begin with the view and create a FXML file, which you can design as you like, just add the following attributes to the fxml file:
  • "fx:controller" to the root panel - this will define the java controller file which relates to this fxml file and recognizes all of it's public (fx:id) components
  • "fx:id" to every component which you will want to reference from the controller
  • "onAction" to every actionable component (buttons?) you will like to add business logic to

Now on the java controller file you should: 
  • Create members of every component on which you put the fx:id tag (annotate them with @FXML)
  • Create methods for every "onaction" attributes from the fxml file (annotate those also with @FXML)

Lastly, create a java class which extends "Application", and which will start the application, which means that it will show the gui.


In a Nutshell

Your application class calls the FXML gui.
Your FXML gui refers the JVM to the controller class using the fx:controller attribute
Your controller recognizes all tagged components from the fxml file as well as implements all of the onAction methods defined in the FXML.

That's it, you don't need to create a special JavaFX project, add special javaFX files etc, the above will suffice for all of your needs.



Important Links

Comments

Popular posts from this blog

Profiling Java @ 2019

Ant Explorer graphical Ant build tool

Start Working with AutoIT