【问题标题】:Javafx Cascading dropdown based on selection基于选择的Javafx级联下拉菜单
【发布时间】:2013-12-27 03:35:44
【问题描述】:

我正在从 swing 迁移到 javafx。任何人都可以提供有关如何根据 javafxe.g 中的父子选择级联组合框的链接/代码 sn-p 帮助吗?国家-州,分支-部门-单位。

【问题讨论】:

    标签: combobox javafx cascading


    【解决方案1】:

    使用此代码:用于基本下拉示例

    package comboboxexamplestackoverflow;
    
    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.ComboBox;
    import javafx.scene.layout.HBox;
    import javafx.stage.Stage;
    
    /**
     *
     * @author reegan
     */
    public class ComboBoxExampleStackOverFlow extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            HBox box = new HBox(20);
            box.setPadding(new Insets(10, 10, 10, 10));
            ObservableList<String> countries =
                    FXCollections.observableArrayList(
                    "India",
                    "Germany",
                    "Israel");
    
            final ObservableList<String> indiaStates =
                    FXCollections.observableArrayList(
                    "StatesIndia1",
                    "StatesIndia2",
                    "StatesIndia3");
    
            final ObservableList<String> germanyStates =
                    FXCollections.observableArrayList(
                    "StatesGermany1",
                    "StatesGermany2",
                    "StatesGermany3");
    
            final ObservableList<String> israelStates =
                    FXCollections.observableArrayList(
                    "StatesIsrael1",
                    "StatesIsrael2",
                    "StatesIsrael3");
    
    
    
            ComboBox comboBox1 = new ComboBox(countries);
            final ComboBox comboBox2 = new ComboBox();
            comboBox1.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
                @Override
                public void changed(ObservableValue ov, Object t, Object t1) {
    
                    switch (t1.toString()) {
                        case "India":
                            comboBox2.setItems(indiaStates);
                            break;
                       case "Germany":
                            comboBox2.setItems(germanyStates);
                            break;
                       case "Israel":
                            comboBox2.setItems(israelStates);
                           break;
    
                    }
                }
            });
            box.getChildren().addAll(comboBox1, comboBox2);
            Scene scene = new Scene(box, 300, 250);
    
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        /**
         * The main() method is ignored in correctly deployed JavaFX application.
         * main() serves only as fallback in case the application can not be
         * launched through deployment artifacts, e.g., in IDEs with limited FX
         * support. NetBeans ignores main().
         *
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    【讨论】:

    • 感谢您的代码。真的有帮助。修改它以使用数据源。我注意到在选择父母(国家组合框)时,孩子(国家组合框)失去了提示,例如“--SELECT STATES--”。 combobox.setPromptText("--SELECT STATES--") 实际上修复了它。
    【解决方案2】:

    与在 Swing 中的操作方式基本相同。
    使用 combo.getSelectionModel().selectedItemProperty().addListener() 添加一个 ChangeListener,只要组合框的选择发生变化,就会调用它。
    Combo Box 的基本介绍见这里:http://docs.oracle.com/javafx/2/ui_controls/combo-box.htm#BABJCCIB

    【讨论】:

      猜你喜欢
      • 2013-09-14
      • 2012-10-27
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 2011-03-03
      相关资源
      最近更新 更多