簡體   English   中英

JavaFX Fire TextField ActionEvent手動| 以編程方式

[英]JavaFX Fire TextField ActionEvent manually | programmatically

我知道一些避免這種情況的方法,例如在不同的節點中添加相同的ActionHandler:

        // Custom Event Handler
        EventHandler<ActionEvent> myHandler = e -> {

             //code

        };

        inputField.setOnAction(myHandler);

        // okButton
        okButton.setOnAction(myHandler);

但是我可以在Node類中看到一個方法fireEvent(Event) 。問題是我有多個TextFields ,當Button名為okButtonButton時,我想一次將它們全部觸發。

那怎么辦? 如果這對您沒有太大意義,請說出於研究原因我想要它:)。

我確實沒有看到這樣做的任何正當理由:您基本上是在要求控制器在視圖上調用用戶事件,而不是照常執行並更新模型。

所以我想我能想到的最接近的東西就是這樣。 假設您(使用規范的JavaFX示例)擁有一個Person類:

public class Person {

    private final StringProperty firstName = new SimpleStringProperty() ;
    private final StringProperty lastName = new SimpleStringProperty() ;
    private final StringProperty email = new SimpleStringProperty() ;

    public StringProperty firstNameProperty() {
        return firstName ;
    }

    public final String getFirstName() {
        return firstNameProperty().get();
    }

    public final void setFirstName(String firstName) {
        firstNameProperty().set(firstName);
    }

    // etc etc
}

然后是某種編輯。 您可以在編輯器中創建一個映射,將每個文本字段映射到一個要執行的動作,然后向每個調用相應動作的文本字段注冊一個事件處理程序。 您的按鈕可以遍歷地圖並調用所有操作:

public class PersonEditor {

     private TextField firstNameTextField ;
     private TextField lastNameTextField ;
     private TextField emailTextField ;

     private Button okButton ;


     public PersonEditor(Person person) {

         // create controls, do layout, etc etc...

         // map textfields to an action on the person:
         Map<TextField, Consumer<String>> actions = new HashMap<>();
         actions.put(firstNameTextField, person::setFirstName);             
         actions.put(lastNameTextField, person::setLastName);             
         actions.put(emailTextField, person::setEmail);

         // set individual event handlers on each text field:
         actions.entrySet().forEach(entry -> {
             TextField tf = entry.getKey();
             tf.setOnAction(e -> entry.getValue().accept(tf.getText()));
         });

         // event handler for button:
         okButton.setOnAction(e -> {
             // invoke action on each text field:
             actions.entrySet().forEach(entry -> entry.getValue().accept(entry.getKey().getText()));
             // do other stuff if needed...
         }

    }

}

根據OP的要求,我正在添加我的評論作為答案。

您可以使用.fireEvent(new ActionEvent())觸發TextField的動作處理程序。

我完成此操作的方法是存儲/獲取所有TextField ,然后在按下Button時,遍歷每個對象和.fireEvent(new ActionEvent()); 下面的小型控制器示例。 VBox來自FXML文件。

public class Controller {

    @FXML private VBox root;
    private List<TextField> all = new ArrayList<>();

    public Controller(){}
    @FXML
    public void initialize(){
        EventHandler<ActionEvent> customEvent = e -> {
            if(e.getSource().getClass() == Button.class){
                all.forEach(t -> t.fireEvent(new ActionEvent()));
            }else {
                System.out.println(((TextField) e.getSource()).getText());
            }
        };

        for(int i=0; i < 10; i++){
            TextField tf = new TextField(Integer.toString(i));
            tf.setOnAction(customEvent);
            all.add(tf);
            root.getChildren().add(tf);
        }
        Button btn = new Button();
        btn.setOnAction(customEvent);
        root.getChildren().add(btn);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM