我有10個切換按鈕說tb1,tb2,..等等。每個人都有一個用戶數據附加到它。我需要附加一個偵聽器來執行一個動作(指令的後綴),當按鈕被點擊和取消點擊時會被執行。我寧願聽者是通用的(可用於所有按鈕)。JavaFX中的ToggleButtons與通用偵聽器
我面臨的問題是,如何訪問監聽器中單擊按鈕的用戶數據。請通過此幫助。
@FXML
private ToggleButton tb1;
@FXML
private ToggleButton tb2;
String cpuLoad1 ="D:/myWorkspace/TestAttacks/input_folder/app_debug.apk";
String cpuLoad2 = "D:/myWorkspace/TestAttacks/input_folder/cpuLoad1.apk";
public void initialize(){
tb1.setUserData(cpuLoad1);
tb2.setUserData(cpuLoad2);
ChangeListener clt1 = new ChangeListener() {
public void changed(ObservableValue ov,
Object toggle, Object new_toggle){
if(new_toggle.equals(true)){
/*how can I acces togglebutton userdata here.
*I would like to pass it as arguement to this class object*/
App load = new App(buttonClicked.getUserData()); //button clicked could tb1 or tb2
load.installApp();
load.launchApp();
}else{
System.out.println("OFF");
/*how can I acces togglebutton userdata here.
*I would like to pass it as arguement to this class object.*/
App load = new App(buttonClicked.getUserData());
load.unInstallApp();
}
}
};
tb1.selectedProperty().addListener(clt1);
tb2.selectedProperty().addListener(clt1);
}
創建[mcve]並顯示您的問題。 – MBec
更新了問題。 –