Java FXML组合框未填充选项

Java FXML组合框未填充选项,java,javafx,combobox,fxml,scene,Java,Javafx,Combobox,Fxml,Scene,我正在尝试制作一个简单的GUI,其中包含一个组合框,我希望该框包含“Employee”和“Manager”选项。然而,由于某种原因,我的组合框没有被填充,我不知道为什么。以下是我的FXML文件的代码: <AnchorPane fx:id="mainpane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth=&qu

我正在尝试制作一个简单的GUI,其中包含一个组合框,我希望该框包含“Employee”和“Manager”选项。然而,由于某种原因,我的组合框没有被填充,我不知道为什么。以下是我的FXML文件的代码:

<AnchorPane fx:id="mainpane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label layoutX="30.0" layoutY="14.0" text="Database status " />
      <TextField fx:id="username" layoutX="30.0" layoutY="125.0" />
      <Label layoutX="30.0" layoutY="99.0" text="Username" />
      <Label layoutX="30.0" layoutY="174.0" text="Password" />
      <PasswordField fx:id="password" layoutX="30.0" layoutY="200.0" />
      <ComboBox fx:id="combobox" layoutX="30.0" layoutY="268.0" prefWidth="150.0" promptText="Manager/Employee" />
      <Button fx:id="Login" layoutX="61.0" layoutY="338.0" mnemonicParsing="false" text="Login" />
      <Label fx:id="dbstatus" layoutX="186.0" layoutY="14.0" text="Label" />
   </children>
</AnchorPane>
这是我在上面代码中引用的选项枚举:

public enum Option {
Manager, Employee;

 Option(){}

public String value(){
    return name();
}

public static Option fromvalue(String value){
    return valueOf(value); // returns the enum constant of that type

}
使用此代码,当我的组合框下拉时,当前没有选项,如下所示:


如何解决此问题?

将您的
枚举更改为:

  public enum Option {
    MANAGER("Manager"),
    EMPLOYEE("Employee");

    private String option;

    Option(String option) {
        this.option = option;
    }

    public String toString() {
        return option;
    }
}

您必须提供一个更完整的示例

这很有效

FXML:


代码:

public类主扩展应用程序{
静态枚举选项{
雇员、经理
}
公共静态类控制器{
@FXML
TextField用户名;
@FXML
密码字段密码;
@FXML
组合框组合框;
@FXML
按钮登录;
@FXML
标记dbstatus;
}
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公众假期开始(阶段){
stage.setTitle(“枚举的组合框”);
FXMLLoader=newFXMLLoader(getClass().getResource(“/layout.xml”);
试一试{
父根=loader.load();
控制器ctrl=loader.getController();
ctrl.combobox.setItems(FXCollections.observableArrayList(Option.values());
场景=新场景(根);
舞台场景;
stage.show();
}捕获(IOEX异常){
Logger.getLogger(Main.class.getName()).log(Level.SEVERE,null,ex);
}
}
}

提供一个新的选项。问题可能是FXML文件的根元素中没有
fx:controller
属性,但是如果没有完整的示例,就无法知道这是否是问题所在。@James\u D是的,这就是问题所在,我忘记了FXML文件根元素中的controller属性。谢谢。您好:)您的代码中的
dbStatus
是什么?您有
选项类型的组合框吗?像这样
@FXML组合框组合框
  public enum Option {
    MANAGER("Manager"),
    EMPLOYEE("Employee");

    private String option;

    Option(String option) {
        this.option = option;
    }

    public String toString() {
        return option;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.Button?>
<AnchorPane fx:controller="stackoverflow.answers.demo.Main$Controller" fx:id="mainpane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label layoutX="30.0" layoutY="14.0" text="Database status " />
      <TextField fx:id="username" layoutX="30.0" layoutY="125.0" />
      <Label layoutX="30.0" layoutY="99.0" text="Username" />
      <Label layoutX="30.0" layoutY="174.0" text="Password" />
      <PasswordField fx:id="password" layoutX="30.0" layoutY="200.0" />
      <ComboBox fx:id="combobox" layoutX="30.0" layoutY="268.0" prefWidth="150.0" promptText="Manager/Employee" />
      <Button fx:id="Login" layoutX="61.0" layoutY="338.0" mnemonicParsing="false" text="Login" />
      <Label fx:id="dbstatus" layoutX="186.0" layoutY="14.0" text="Label" />
   </children>
</AnchorPane>
public class Main extends Application {

    static enum Option {
        Employee, Manager
    }

    public static class Controller {
    @FXML
    TextField username;
    @FXML
    PasswordField password;
    @FXML
    ComboBox<Option> combobox;
    @FXML
    Button Login;
    @FXML
    Label dbstatus;
    }

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        stage.setTitle("ComboBox for enum");
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/layout.xml"));
        try {
            Parent root = loader.load();
            Controller ctrl = loader.getController();
            ctrl.combobox.setItems(FXCollections.observableArrayList(Option.values()));
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}