java - 将数组列表添加到我的 javafx 程序中的选择框

标签 java javafx arraylist

所以我一直在尝试将数组列表传输到我在 JavaFX 类中创建的选择框。但是,我在加载项目名称时遇到了一些问题。我创建了一个名为 Item 的单独类,其中包含我创建的构造函数,并且创建了一个返回项目名称的方法。但是,我想将项目属性传输到我的选择框,但只显示项目名称。

public class Item {


    private final String name, description;
    private boolean spoiled, buyable;
    private final int price, nutrition;
    private final boolean isFood;

    private double hoursToRot = 15;

    public Item() {
        this.name = null;
        this.description = null;
        this.price = 0;
        this.nutrition = 0;
        this.isFood = false;
    }
    public Item(String name, String description, int price, boolean buyable, boolean isFood, int nutrition)
    {

       this.description = description;
       this.name = name;      
       this.price = price;
       this.buyable = buyable;
       this.isFood = isFood;
       this.nutrition = nutrition;

    }
    public String getName()
    {
        return name;
    }
...

如您所见,这是我的项目类别

public class Game 
{
    Item item = new Item();
       public ArrayList<Item> supermarkedItems = new ArrayList();     
     public void supermarkedItems() {


        Item meat, milk, cake, rice, ryebread;

        milk = new Item("milk", "This is milk!", 14, true, true, 20);
        meat = new Item("meat", "This is meat!", 35, true, true, 30);
        cake = new Item("cake", "This is a whole cake!", 60, true, true, 50);
        rice = new Item("rice", "This is 500g of white rice!", 25, true, true, 50);
        ryebread = new Item("ryebread", "This is a loaf of ryebread", 25, true, true, 25);

        supermarkedItems.add(meat);
        supermarkedItems.add(milk);
        supermarkedItems.add(cake);
        supermarkedItems.add(rice);
        supermarkedItems.add(ryebread);



     }

     public ArrayList<Item> getsupermarkedList() {
         return supermarkedItems;
     }
     public String getItemNames () {
         return item.getName();
     }
...

这是我的游戏类(class)。 在我的 javafx 程序中,我创建了一个单例类来将我的内容传输到我的 JavaFX 类。

public class OrderTableController implements Initializable {

    Game g1 = Game.getInstance();
    @FXML
    private ChoiceBox<Item> cbItems = new ChoiceBox<Item>(FXCollections.observableArrayList(g1.getsupermarkedList())); 
    @Override
    public void initialize(URL url, ResourceBundle rb) {

        cbItems.getItems().addAll(g1.getItemNames());


...

但是由于某种原因,我确实无法将我的 Items 传输到我在 choicebox 类中创建的新 observableArrayList。 These are the errors i receive

最佳答案

您收到错误是因为您尝试将 String 类型的对象添加到应包含 Item 类型的列表中。您只能添加 Item 类或扩展类 Item 的对象。

要选择要在 ChoiceBox 中显示的对象文本,您需要重写类中的 toString()

您可以将其添加到 Item 类中以在 ChoiceBox 中显示名称:

@Override
public String toString() {
    return name;
}

您可以通过执行以下操作将项目添加到 ChoiceBox:

cbItems.getItems().addAll( g1.getsupermarkedList() );

关于java - 将数组列表添加到我的 javafx 程序中的选择框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59238115/

相关文章:

combobox - 比较 2 个组合框的项目

java - 读取数组列表以查找特定字符串和值的方法

java - 使用 BufferedReader 进行递归

java - 使用sql查询将varchar转换为int

java - 二叉树的有序迭代器

java - Apache PDFBox 拒绝打开临时创建的 PDF 文件

java - 无法将 JSONObjects 的 ArrayList 传递给新 Activity

java - 如何让我的 fxml 和 java 类协同工作?

java - 拆分字符串 Java(在特定字符处)

java - 带有翻译的 FXML 选择框