基于选择的Javafx级联下拉列表 [英] Javafx Cascading dropdown based on selection
本文介绍了基于选择的Javafx级联下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从swing迁移到javafx。任何人都可以帮助有关如何级联组合框的链接/代码片段基于javafxe.g中的父子选择。
am migrating from swing to javafx. Can anyone help with a link/code snippet on how to cascade combobox(es) based on parent-child selection in javafxe.g. country-state, branch-department-unit.
推荐答案
使用此代码:基本下拉示例
Use this code:for basic drop down example
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);
}
}
这篇关于基于选择的Javafx级联下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文