2017-03-02 78 views
1

我有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); 

} 
+1

創建[mcve]並顯示您的問題。 – MBec

+0

更新了問題。 –

回答

1

您有幾種選擇。

選項1:收集ToggleButton s轉換的集合,並且在聽者使用參考:

List<ToggleButton> toggles = new ArrayList<>(Arrays.asList(tb1, tb2)); 

for(ToggleButton toggle:toggles) 
    toggle.selectedProperty().addListener((observable, oldValue, newValue) -> 
     System.out.println(toggle.getText() + " - Selected: " + toggle.isSelected() + "; UserData: " + toggle.getUserData())); 

選項2:可以使用onActionProperty

tb1.setOnAction(e -> { 
    ToggleButton toggle = (ToggleButton) e.getSource(); 
    System.out.println(toggle.getText() + " - Selected: " + toggle.isSelected() 
      + "; UserData: " + toggle.getUserData()); 
}); 

選項3:如果您需要存儲聽衆,則可以實施您自己的ChangeListener

public class Toggles extends Application { 
    public static void main(String[] args) { launch(args); } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 

     VBox vBox = new VBox(); 
     for (int i = 0; i < 20; i++) { 
      ToggleButton tb = new ToggleButton("ToggleButton" + i); 
      tb.setUserData("UserData" + i); 
      tb.selectedProperty().addListener(new ToggleButtonSourcedChangeListener(tb)); 
      vBox.getChildren().add(tb); 
     } 

     Scene scene = new Scene(new BorderPane(vBox), 320, 240); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    private abstract static class SourcedChangeListener<T extends Node> implements ChangeListener<Boolean> { 
     T source; 

     SourcedChangeListener(T source) { 
      this.source = source; 
     } 
    } 

    private static class ToggleButtonSourcedChangeListener extends SourcedChangeListener<ToggleButton> { 

     ToggleButtonSourcedChangeListener(ToggleButton source) { 
      super(source); 
     } 

     @Override 
     public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) { 
      System.out.println(source.getText() + " - Selected: " + source.isSelected() 
        + "; UserData: " + source.getUserData()); 
     } 
    } 
} 

在這種SSCE我創建一個抽象SourceChangeListener可以由具體實現進行擴展。通用參數<T extends Node>背後的意圖是避免強制轉換。

當你執行這個代碼,點擊切換,輸出將是這樣的:

ToggleButton4 - Selected: true; UserData: UserData4 
ToggleButton5 - Selected: true; UserData: UserData5 
ToggleButton4 - Selected: false; UserData: UserData4 
ToggleButton8 - Selected: true; UserData: UserData8 
ToggleButton5 - Selected: false; UserData: UserData5 
ToggleButton2 - Selected: true; UserData: UserData2 

我建議的是使用selectedProperty選項之一爲onActionProperty將改變只有當按鈕被按下(通過鼠標,觸摸或鍵)或者如果您以編程方式調用fire()方法。即使您以編程方式更改選定狀態,其他兩個選項也會始終工作。

+0

他們所有的作品......非常感謝你。 –

+0

您不需要保留對切換的引用 - 所選屬性(與其他任何屬性一樣)具有屬性屬主的字段bean。並強烈建議對onAction,你已經提到了爲什麼:-) – kleopatra

相關問題