2

I have successfully installed e(fx)clipse plugin to eclipse and Scenebuilder 8.0, they are good integrated. I have previously always used and developed GUI applications with Swing, with help of WindowBuilder plugin, it was very easy and convenient but now I have to develop an app with JavaFX, that is the reason I installed SceneBuilder. In WindowBuilder for example when I right clicked a button it was possible to add an action to it and then switch to code and develop simultaneously, the action block was created automatically under the button in code. But in Scene Builder when I want to add an action and edit it in code I fail here is how it looks by me:

My Scene Builder

Below is how it looks on the examples I find on the internet:

Scene Builder from Internet Tutorial

I don't see an arrow for choosing actions or handleButtonAction. I just want to use SceneBuilder like the good old WindowBuilder, is this possible? Could you enlighten me, what am I doing wrong?

Anarkie
  • 657
  • 3
  • 19
  • 46

1 Answers1

3

WindowBuilder creats Java code by dragging and dropping UI elements, where as SceneBuilder creates FXML. Therefore, we do not have an option to add an action to it and then directly switch to code.

Swing allowed us to create UI using Java code, which made it very difficult for designers to make changes to the view. JavaFX takes a more designer friendly approach by separating the view from the code. The view in JavaFX is a separate entity and can be designed / altered using CSS and FXML.

James_D has said in one of his comments :

JavaFX follows more of a MVP-style architecture, where the view is basically passive and is manipulated by the presenter ("controller").

Basically, each view in JavaFX has a corresponding Controller(Java File) for it.

This controller is used for the following things :

  1. Instantiate the controls and layouts defined in the FXML along with the properties defined for them using the fx:id.
  2. Bind the controls events to proper handlers whose names are defined in the FXML and body is defined in the controller.

The FXML is loaded using the FXMLLoader which in turn instantiates the Controller. The controller class is identified by fx:controller in FXML.

How do you handle actions of the UI elements ?

You add the actions as methods inside your controller.

For the above FXML that you have created with a Button whose onAction is handleButtonAction, you need to create a method with this name inside your controller.

public void handleButtonAction(ActionEvent action) {

   // Do you action 

}

Some good reads :

Community
  • 1
  • 1
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • Thanks a lot for your answer, now it is working but does this mean I have to manually create all actions and assign them also manually one by one to buttons etc.? – Anarkie Oct 21 '15 at 12:31
  • 1
    This is very inconvenient compared to WindowBuilder, is there no work around a trick or something like that to ease things? – Anarkie Oct 22 '15 at 13:59