#Is there a way to create a "Generic controller class" in JavaFX?

11 messages · Page 1 of 1 (latest)

mighty oriole
#

I'm working on a JavaFX application, and I have an array of buttons. I want to create a "generic" controller class that can handle events for all the buttons in the array. Is there a way to achieve this?

More info:
In my JavaFX application, I have an array of buttons (Button[] arrayButtons). I also have an FXML file (buttonItem.fxml) that defines the layout and appearance of each button. Currently, I'm setting up individual event handlers for each button with a loop. However, this approach leads to repetitive code, and I'd like to create a more efficient and maintainable solution.

lean prawnBOT
#

This post has been reserved for your question.

Hey @mighty oriole! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

hardy dragon
#

Are you using FXML?

mighty oriole
hardy dragon
#

So in your controller, you define a method

#

and in your FXML file, you can specify the method for a specific button

#

and when that button is clicked, the method is executed

mighty oriole
#

Thank you for the help

lean prawnBOT
# mighty oriole Thank you for the help

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.