How to perform several actions for a button depending on the choice of ComboBox in JavaFX

This is the main class for creating a user interface:

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



}

}

This is a button handler class

public class ButtonHandlerClass implements EventHandler<ActionEvent> {

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



}

}

I want the same search button to perform a different action depending on which user has selected in the combo box. I tried to do something similar to ButtonHandlerClass for the combo box. Any advice would be appreciated.

Thank!

+4
source share
2 answers

Save this event handler for way 1and way 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
                }
            };

Method 1 :

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

Path 2 (avoid creating anonymous classes):

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

Method 3:

( , ) (, ...)

[ , way 1 way 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, External
  • // 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  
    
      }
    
     }
    
    }
    
+3

?

@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);
}
+2

Source: https://habr.com/ru/post/1660250/


All Articles