fxml组合框未填充选项

aelbi1ox  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(357)

我试图制作一个简单的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 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

}

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

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

ou6hu8tu

ou6hu8tu1#

你必须提供一个更完整的例子。
这很有效。。。
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);
        }
    }
}
kuarbcqp

kuarbcqp2#

改变你的想法 Enum 收件人:

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

    private String option;

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

    public String toString() {
        return option;
    }
}

相关问题