【问题标题】:FXML combobox not populating with optionsFXML 组合框未填充选项
【发布时间】:2021-03-05 12:32:00
【问题描述】:

我正在尝试制作一个包含组合框的简单 GUI,并且我希望该框具有“员工”和“经理”选项。但是,由于某种原因,我的组合框没有被填充,我不确定为什么。这是我的 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 void initialize(URL url, ResourceBundle RB){

    if (this.loginmodel.isDBconnected()){
        this.dbstatus.setText("Connected");
    }
    else{
        this.dbstatus.setText("Not connected");
    }

    this.combobox.setItems(FXCollections.observableArrayList(Option.values())); //this piece sets the options of the combo box
}

这是上面代码中引用的我的选项枚举:

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

}

使用此代码,当我的组合框下拉时,当前没有选项,它看起来像这样:

我能做些什么来解决这个问题?

【问题讨论】:

  • 提供minimal reproducible example可能问题是您的 FXML 文件的根元素中没有 fx:controller 属性,但是如果没有完整的示例,就不可能知道这是否是问题所在。
  • @James_D 是的,这就是问题所在,我忘记了 FXML 文件根目录中的控制器属性。谢谢。
  • 您好 :) 您的代码中的 dbStatus 是什么?

标签: java javafx combobox fxml scene


【解决方案1】:

你必须提供一个更完整的例子。

这行得通...

FXML:

<?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);
        }
    }
}

【讨论】:

    【解决方案2】:

    将您的 Enum 更改为:

      public enum Option {
        MANAGER("Manager"),
        EMPLOYEE("Employee");
    
        private String option;
    
        Option(String option) {
            this.option = option;
        }
    
        public String toString() {
            return option;
        }
    }
    

    【讨论】:

    • 您有Option 类型的组合框吗?像这样@FXML ComboBox&lt;Option&gt; combobox;
    猜你喜欢
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多