如何根据JavaFX中的ComboBox选择为Button执行多个操作

时间:2016-11-09 01:22:33

标签: java javafx actionevent

这是创建用户界面的主要类:

public class Test extends Application {

@Override
public void start(Stage primaryStage) {

    FlowPane mainPane = new FlowPane();         


    FlowPane query = new FlowPane();            
    query.setPadding(new Insets(30,30,30,30));
    query.setHgap(10);
    query.setVgap(20);

    ComboBox<String> queryDropDown = new ComboBox<>();      
    queryDropDown.getItems().addAll("Gene", "Disease");
    queryDropDown.setValue("Select One");
    System.out.println(queryDropDown.getValue());


    query.getChildren().addAll(new Label("Select Category: "), queryDropDown);


    FlowPane userInput = new FlowPane();            
    userInput.setPadding(new Insets(30,30,30,30));
    userInput.setHgap(10);
    userInput.setVgap(20);

    TextField searchField = new TextField();    
    searchField.setPrefColumnCount(3);
    userInput.getChildren().addAll(new Label("Enter Query: "), new TextField());




    FlowPane searchButtonPane = new FlowPane();         

    searchButtonPane.setPadding(new Insets(30,30,30,200));
    searchButtonPane.setHgap(50);
    searchButtonPane.setVgap(50);
    Button searchButton = new Button("Search");

    searchButtonPane.getChildren().addAll(searchButton);

    ButtonHandlerClass handler1 = new ButtonHandlerClass();     
    searchButton.setOnAction(handler1);


    mainPane.getChildren().addAll(query, userInput, searchButtonPane);      

    Scene scene = new Scene(mainPane, 300, 250);
    primaryStage.setTitle("Genetic Database");
    primaryStage.setScene(scene);
    primaryStage.show();





}


public static void main(String[] args) {
    // Prints "Hello, World" to the terminal window.
    System.out.println("Hello, World");
    Application.launch(args);



}

}

这是按钮处理程序类

public class ButtonHandlerClass implements EventHandler<ActionEvent> {

@Override
public void handle(ActionEvent e) {
    System.out.println("Button Clicked");



}

}

我希望能够使用相同的“搜索”按钮执行不同的操作,具体取决于用户在组合框中选择的选项。我尝试过类似于组合框的ButtonHandlerClass。任何建议将不胜感激。

谢谢!

2 个答案:

答案 0 :(得分:3)

way 1way 3保留此事件处理程序:

           // Using an event handler
            EventHandler<ActionEvent> handler = new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent arg0) {
                    String selectedItem = queryDropDown.getSelectionModel().getSelectedItem();
                    System.out.println(selectedItem);
                    // ...code
                }
            };

方式1

        //note that every time one action event handler is called
        //instead for multiple handlers you can use the way 3
        searchButton.setOnAction(handler);

方式2 (避免创建匿名类):

    //using lambda expression
    searchButton.setOnAction(a->{
        String selectedItem = queryDropDown.getSelectionModel().getSelectedItem();
        System.out.println(selectedItem);
        // ...code      
    });

方式3:

你可以添加多个事件处理程序,所有人都会被调用)(例如你可以添加多个动作事件处理程序......

[这是way 1way 2]无法做到的事情:

    //adding an event handler
    searchButton.addEventHandler(ActionEvent.ACTION,handler);

    //or

    searchButton.addEventHandler(ActionEvent.ACTION,a->{
        String selectedItem = queryDropDown.getSelectionModel().getSelectedItem();
        System.out.println(selectedItem);
        // ...code      
    });

方式4:

使用外部类(不推荐使用,除非您有太多代码,不希望当前类的行太多);()

这可能是不同的情况:

  • 该类将嵌套到您当前的类中?
  • ComboBox是全局变量,可以从嵌套中访问 类?
  • 如果!2那么你必须在构造函数上传递它的引用 外部课程
  • 有一百万种情况

    //情况3代码:

     public class ButtonHandlerClass implements EventHandler<ActionEvent> {
    
       ComboBox<String> comboBox;
    
       /**
       *Constructor
       */
       public ButtonHandlerClass(ComboBox comboBox){
    
         this.comboBox = comboBox;
    
      }
    
     @Override
      public void handle(ActionEvent e) {
         String selectedItem = queryDropDown.getSelectionModel().getSelectedItem();
                System.out.println(selectedItem);
                // ...code  
    
      }
    
     }
    
    }
    

答案 1 :(得分:2)

为什么不使用开关声明?

@Override
public void start(Stage primaryStage) {

    VBox vbox = new VBox();
    ComboBox<String> comboBox = new ComboBox();
    ObservableList<String> options = FXCollections.observableArrayList("one", "two", "three");
    comboBox.setItems(options);
    Button btn = new Button();
    btn.setText("Say 'Hello World'");
    btn.setOnAction((ActionEvent event) -> {
        if(comboBox.getValue() != null)
        {
            String tempString = comboBox.getSelectionModel().getSelectedItem();
            System.out.print(tempString);
            switch(tempString)
            {
                case "one":
                    btn.setText("one is selected in combobox");
                    break;
                case "two":
                    btn.setText("two is selected in combobox");
                    break;
                case "three":
                    btn.setText("three is selected in combobox");
                    break;
                default:
                    btn.setText("Nothing is selected in combobox");
            }
        }
    });

    StackPane root = new StackPane();
    vbox.getChildren().addAll(btn, comboBox);
    root.getChildren().addAll(vbox);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}