如何在JavaFx中将列表值填充到组合框中 [英] How to populate a list values to a combobox in JavaFx
本文介绍了如何在JavaFx中将列表值填充到组合框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个值列表,我希望在javaFx中的组合框中填充这些值。这是我的combo.xml
I have a list of values which i want to populate in a combobox in javaFx. this is my combo.xml
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="- Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<ComboBox id="comboId" layoutX="210.0" layoutY="108.0" prefHeight="27.0" prefWidth="102.0" promptText="Select">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Item 1" />
<String fx:value="Item 2" />
<String fx:value="Item 3" />
</FXCollections>
</items>
</Com boBox>
</children>
</AnchorPane>
这是我的主要货币
public class JavaFXExperiment extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("combo.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
final ComboBox comboId = new ComboBox();
comboId.getItems().addAll(
"jacob.smith@example.com",
"isabella.johnson@example.com",
"ethan.williams@example.com",
"emma.jones@example.com",
"michael.brown@example.com");
}
public static void main(String[] args) {
launch(args);
}
}
这是我的xml文件和我想要的主类要在组合框中显示这些值,请帮助
This is my xml file and the main class i want to show those values in the combobox.anyone please help
推荐答案
您必须创建一个控制器并使用FXML屏幕进行分配。
You have to create one controller and assign it with your FXML Screen.
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="- Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" fx:controller="MyController" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<ComboBox fx:id="myCombobox" id="comboId" layoutX="210.0" layoutY="108.0" prefHeight="27.0" prefWidth="102.0" promptText="Select">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Item 1" />
<String fx:value="Item 2" />
<String fx:value="Item 3" />
</FXCollections>
</items>
</ComboBox>
</children>
</AnchorPane>
那么你的主要课程将是,
Then your main class will be,
public class JavaFXExperiment extends Application {
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("combo.fxml"));
Parent root = loader.load();
MyController myController = loader.getController();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
//Set Data to FXML through controller
myController.setData();
}
public static void main(String[] args) {
launch(args);
}
}
并且您的控制器将是,
public class MyController implements Initializable
{
@FXML
public Combobox myCombobox;
@Override
public void initialize(URL url, ResourceBundle rb) {
}
public void setData(){
myCombobox.getItems().clear();
myCombobox.getItems().addAll(
"jacob.smith@example.com",
"isabella.johnson@example.com",
"ethan.williams@example.com",
"emma.jones@example.com",
"michael.brown@example.com");
}
}
这篇关于如何在JavaFx中将列表值填充到组合框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文