如何在JavaFX中将方法绑定到组合框?

时间:2017-02-15 22:45:43

标签: javafx

我的.fxml

中有这个简单的组合框
<ComboBox fx:id="comboShapes" prefWidth="150.0" value="Random">
    <items>
        <FXCollections fx:factory="observableArrayList">
            <String fx:value="Random" />
            <String fx:value="Small Exploder" />
            <String fx:value="Gospler Glider Run" />
            <String fx:id="comboGlider" fx:value="Glider" />
         </FXCollections>
   </items>
</ComboBox>

我想知道如何将set方法绑定到Controller中的不同choicebox字符串?

1 个答案:

答案 0 :(得分:4)

  1. 在您的控制器中为您的组合框定义onAction(ActionEvent event)操作处理程序。
  2. 通过onAction="#choiceMade"将fxml链接到控制器操作处理程序(其中choiceMade是我为操作处理程序方法选择的任意名称)。
  3. 在您的操作处理程序中,对组合框值执行切换以调用所选值的相应方法处理程序。
  4. pic

    应用/ ComboAction.fxml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import java.lang.String?>
    <?import javafx.collections.FXCollections?>
    <?import javafx.geometry.Insets?>
    <?import javafx.scene.control.ComboBox?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.layout.VBox?>
    
    <VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="104.0" prefWidth="226.0" spacing="10.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ComboActionController">
       <children>
          <Label fx:id="choiceLabel" />
          <ComboBox fx:id="comboShapes" onAction="#choiceMade" prefWidth="150.0" value="Random">
             <items>
                <FXCollections fx:factory="observableArrayList">
                   <String fx:value="Random" />
                   <String fx:value="Small Exploder" />
                   <String fx:value="Gospler Glider Run" />
                   <String fx:value="Glider" />
                </FXCollections>
             </items>
          </ComboBox>
       </children>
       <padding>
          <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
       </padding>
    </VBox>
    

    应用/ ComboActionApp.java

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class ComboActionApp extends Application {
        @Override
        public void start(Stage stage) throws Exception {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("ComboAction.fxml"));
            Parent root = loader.load();
            stage.setScene(new Scene(root));
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    应用/ ComboActionController.java

    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.Label;
    
    public class ComboActionController {
    
        @FXML
        private ComboBox<String> comboShapes;
    
        @FXML
        private Label choiceLabel;
    
        @FXML
        void choiceMade(ActionEvent event) {
            String choice = comboShapes.getValue();
    
            choiceLabel.setText(
                    "Chosen: " + choice
            );
    
            switch (choice) {
                case "Random":
                    randomChosen();
                    break;
    
                case "Small Exploder":
                    smallExploderChosen();
                    break;
    
                case "Gospler Glider Run":
                    gliderChosen();
                    break;
            }
        }
    
        private void gliderChosen() {
            // todo implement action handler.
        }
    
        private void smallExploderChosen() {
            // todo implement action handler.
        }
    
        private void randomChosen() {
            // todo implement action handler.
        }
    
    }