ActionEvent get source of button JavaFX -


i have 10 buttons going sent same method. want method identify source. method knows button "done" has evoked function. can add switch case of if statement handle them accordingly. have tried

//call:     btndone.setonaction(e -> test(e));      public void test(actionevent e) {         system.out.println("action 1: " + e.gettarget());         system.out.println("action 2: " + e.getsource());         system.out.println("action 3: " + e.geteventtype());         system.out.println("action 4: " + e.getclass());     } 

output result:

action 1: button@27099741[styleclass=button]'done' action 2: button@27099741[styleclass=button]'done' action 3: action action 4: class javafx.event.actionevent 

done text on button. can see use e.gettarget() and/or e.getsource() i'll have substring it, "done" appears. there other way string in apostrophe instead of having substring.

update: have tried passing button , works still want know solution using actionevent.

//call:         btndone.setonaction(e -> test(btndone));          public void test(button e) {             system.out.println("action 1: " + e.gettext());         } 

output action 1: done

typically prefer using different method each button. it's bad idea rely on text in button (e.g. happen logic if want internationalize application?).

if want text in button (and again, have emphasize don't want this), use downcast:

string text = ((button)e.getsource()).gettext(); 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -