fxml组合框未填充选项

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

我试图制作一个简单的gui,其中包含一个组合框,我希望这个框有“employee”和“manager”选项。但是,由于某些原因,我的组合框没有被填充,我不知道为什么。以下是我的fxml文件的代码:

  1. <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">
  2. <children>
  3. <Label layoutX="30.0" layoutY="14.0" text="Database status " />
  4. <TextField fx:id="username" layoutX="30.0" layoutY="125.0" />
  5. <Label layoutX="30.0" layoutY="99.0" text="Username" />
  6. <Label layoutX="30.0" layoutY="174.0" text="Password" />
  7. <PasswordField fx:id="password" layoutX="30.0" layoutY="200.0" />
  8. <ComboBox fx:id="combobox" layoutX="30.0" layoutY="268.0" prefWidth="150.0" promptText="Manager/Employee" />
  9. <Button fx:id="Login" layoutX="61.0" layoutY="338.0" mnemonicParsing="false" text="Login" />
  10. <Label fx:id="dbstatus" layoutX="186.0" layoutY="14.0" text="Label" />
  11. </children>
  12. </AnchorPane>

这段代码应该设置组合框的选项:

  1. public void initialize(URL url, ResourceBundle RB){
  2. if (this.loginmodel.isDBconnected()){
  3. this.dbstatus.setText("Connected");
  4. }
  5. else{
  6. this.dbstatus.setText("Not connected");
  7. }
  8. this.combobox.setItems(FXCollections.observableArrayList(Option.values())); //this piece sets the options of the combo box
  9. }

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

  1. public enum Option {
  2. Manager, Employee;
  3. Option(){}
  4. public String value(){
  5. return name();
  6. }
  7. public static Option fromvalue(String value){
  8. return valueOf(value); // returns the enum constant of that type
  9. }

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

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

ou6hu8tu

ou6hu8tu1#

你必须提供一个更完整的例子。
这很有效。。。
fxml地址:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.layout.AnchorPane?>
  3. <?import javafx.scene.control.Label?>
  4. <?import javafx.scene.control.ComboBox?>
  5. <?import javafx.scene.control.TextField?>
  6. <?import javafx.scene.control.PasswordField?>
  7. <?import javafx.scene.control.Button?>
  8. <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">
  9. <children>
  10. <Label layoutX="30.0" layoutY="14.0" text="Database status " />
  11. <TextField fx:id="username" layoutX="30.0" layoutY="125.0" />
  12. <Label layoutX="30.0" layoutY="99.0" text="Username" />
  13. <Label layoutX="30.0" layoutY="174.0" text="Password" />
  14. <PasswordField fx:id="password" layoutX="30.0" layoutY="200.0" />
  15. <ComboBox fx:id="combobox" layoutX="30.0" layoutY="268.0" prefWidth="150.0" promptText="Manager/Employee" />
  16. <Button fx:id="Login" layoutX="61.0" layoutY="338.0" mnemonicParsing="false" text="Login" />
  17. <Label fx:id="dbstatus" layoutX="186.0" layoutY="14.0" text="Label" />
  18. </children>
  19. </AnchorPane>

代码:

  1. public class Main extends Application {
  2. static enum Option {
  3. Employee, Manager
  4. }
  5. public static class Controller {
  6. @FXML
  7. TextField username;
  8. @FXML
  9. PasswordField password;
  10. @FXML
  11. ComboBox<Option> combobox;
  12. @FXML
  13. Button Login;
  14. @FXML
  15. Label dbstatus;
  16. }
  17. public static void main(String[] args) {
  18. launch(args);
  19. }
  20. @Override
  21. public void start(Stage stage) {
  22. stage.setTitle("ComboBox for enum");
  23. FXMLLoader loader = new FXMLLoader(getClass().getResource("/layout.xml"));
  24. try {
  25. Parent root = loader.load();
  26. Controller ctrl = loader.getController();
  27. ctrl.combobox.setItems(FXCollections.observableArrayList(Option.values()));
  28. Scene scene = new Scene(root);
  29. stage.setScene(scene);
  30. stage.show();
  31. } catch (IOException ex) {
  32. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  33. }
  34. }
  35. }
展开查看全部
kuarbcqp

kuarbcqp2#

改变你的想法 Enum 收件人:

  1. public enum Option {
  2. MANAGER("Manager"),
  3. EMPLOYEE("Employee");
  4. private String option;
  5. Option(String option) {
  6. this.option = option;
  7. }
  8. public String toString() {
  9. return option;
  10. }
  11. }

相关问题