我需要使用MVC填充JAVAFX ComboBox的值,但不知怎的,我无法让组合框工作。请帮帮我,
LoginController.java
package weltes.finance.controller;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
/**
* Created by engineering09 on 12/18/2015.
*/
public class LoginController {
@FXML private ComboBox userComboBox;
@FXML private void fillComboBox(){
userComboBox.getItems().addAll("Item1", "Item2");
}
}
loginscreen.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="163.0" prefWidth="357.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="weltes.finance.controller.LoginController">
<children>
<ComboBox fx:id="userComboBox" layoutX="15.0" layoutY="14.0" prefHeight="25.0" prefWidth="328.0" promptText="Select Registered User" />
<PasswordField layoutX="15.0" layoutY="56.0" prefHeight="25.0" prefWidth="328.0" promptText="Enter Password" />
<Button layoutX="15.0" layoutY="97.0" mnemonicParsing="false" prefHeight="51.0" prefWidth="328.0" text="Button" />
</children>
</AnchorPane>
答案 0 :(得分:1)
首先列出FXML中的项目。
<ComboBox fx:id="userComboBox" editable="false" layoutX="14.0" layoutY="22.0" onAction="#setTimeMultiplier">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Item 1" />
<String fx:value="Item 2" />
<String fx:value="Item 3" />
</FXCollections>
</items>
</ComboBox>
然后在ObservableArrayList中添加或删除项目。
userComboBox.getItems().removeAll(comboTime.getItems());
userComboBox.getItems().addAll(...);