• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to know which button was clicked

 
Ken Matson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Just learning JavaFx and Scene Builder. So I'm successfully presenting a simple form which was designed in Scene Builder. It has several buttons, all of which simply close the form. Great - they work fine. What I need to know is how the method that called/opened this form can know which button was clicked? eg how do I pass back which button was clicked to the calling program?

THANKS!
 
Knute Snortum
Sheriff
Posts: 6029
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about something like this:

* Create an instance variable in the calling class
* Create a setter and getter to that variable
* Pass a reference of the calling class to the called class
* In the called class, use the reference (object) to set the button pressed
 
John Damien Smith
Ranch Hand
Posts: 374
24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at how the standard JavaFX alerts handle this.
Perhaps you can use a similar construct of returning an Optional ButtonType from a showAndWait method defined on the alert.

Here is an example with an Alert:

Code is from:
http://code.makery.ch/blog/javafx-dialogs-official

Or you could instantiate or extend Dialog and make use of its existing ButtonType returning logic and other functionality if that is appropriate for you.
Instantiating a Dialog is what I would recommend if that fits well into your application.
To see how to do this, look at the "Custom Login Dialog" section provided in the makery dialog tutorial link I provided.
And, yeah, I realize the Dialog API is a bit strange and would take some time to get used to.  But it might be worth investing that time in this case in order to be able to make use of the built-in functionality which Dialogs provide.
 
Norm Radder
Rancher
Posts: 3376
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at what is passed to the button press handler?  An Id can be passed there.
 
Ken Matson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it figured out thanks to some online searches. Basically what I'm doing is getting a reference to the controller for the form, and referencing variables there through getters. The buttons themselves Can update the variables. Thanks for the help everyone!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!