1

I am starting to get my hands on JavaFX and I am developing the GUI using the SceneBuilder. I have the following structure on my java project:

src/ # Main class and other classes..
src/[package]/IndexClass
src/[Another package]/ProductClass
src/base.fxml # I am using this as a template

In base.fxml, I have a MenuBar containing a menu item, lets say a Button called btnNew. In SceneBuilder I have declared an onAction eventhandler for that button, and I am trying to hook it up to a method inside ProductClass. However, ProductClass is in a different package as base.fxml and when I click on onAction for the menu item on base.fxml, it appears empty.

My question is, how can I hook the onAction method of a control in Scene Builder to an action which resides on a different package/namespace as the .fxml file?

If you need more clarification let me know and I will take some time to diagram my question.

José Pereda
  • 44,311
  • 7
  • 104
  • 132
Benjamin Vison
  • 469
  • 9
  • 20

1 Answers1

1

You need to hook your FXML file up to a controller.

In the root node of the FXML page, type the following:

fx:controller="package.subpackage.ControllerName"

Where the string is the relative package path of the Controller. Notice that it doesn't matter if the controller is in a different package than the FXML file, you always declare a path from the source package. You can also let Netbeans do this for you, by right-clicking the FXML file and selecting 'Make Controller'.

If you have given any elements an fx:id, JavaFX will inject them into your controller if you declare them with an @FXML tag like this:

@FXML private Button btnNew;

You can declare events in the same way. For example:

@FXML
void btnNew_OnAction(ActionEvent event) {
    //body
}

UPDATE:

I think you should reconsider your design. An FXML file can only be tied to one controller. If you have a menubar, the behaviour of the buttons on that menubar are probably related and I think it's a bad idea to seperate that behaviour into different controller classes. The way I would solve it is to have a single controller that handles all the events for all the buttons in the menubar.

How have you set up your controllers? FXML is a form of MVC(Model-View-Controller). The way I usually do it is to have all the data away from the controllers, in the Model layer. I then have the controllers interact with the data and listen for changes. That way, you don't need to have the event handlers spread out in specific controllers, because they all have access to the model layer. I found this answer that explains this quite well. It injects a Context object into the controllers. A good solution for small-scale applications, but beware of creating a God-object this way though. :)

That said, if you really really want to have multiple controllers for various GUI elements on the same screen, that's totally possible. You need to have a different FXML file for each controller, though. Take a look at this answer, it addresses the issue quite well: creating multiple controllers.

Community
  • 1
  • 1
Reinstate Monica
  • 2,767
  • 3
  • 31
  • 40
  • My concern with this option is that since the button is on the menu bar inside a template FXML I am 100% certain that I will add more buttons to the menu and those buttons will access different controller methods, so if I hook the FXML that has the menu bar to 1 controller I wont be able to hook the rest of the buttons that I will be adding to different methods on different controllers. I know this seems like I may be misunderstanding the structure of JavaFX, but anyway, how would you approach this? – Benjamin Vison Jan 02 '15 at 22:16
  • @BenjaminVison I expanded my answer. Please tell me if this answers your question, or if there is anything that isn't clear yet. – Reinstate Monica Jan 02 '15 at 23:14
  • That's what I thought, thanks for the help I will re-structure what I'm doing to have a controller take care of all those actions. – Benjamin Vison Jan 03 '15 at 15:50