首页 > 解决方案 > 无法在 JavaFx 中使用 MVC 模式绑定 Listview

问题描述

我正在尝试在 javafx 中使用 mvc 模式创建应用程序,但我坚持将数据绑定到 ListView。我正在尝试绑定 storestock listView 中的数据,但是当我运行应用程序时它没有显示任何数据。它只是显示空列表视图。有人可以在这方面帮助我吗?我会感谢你的。

FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="743.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Button fx:id="completeSalebtn" disable="true" layoutX="593.0" layoutY="338.0" mnemonicParsing="false" onAction="#completeAction" prefHeight="45.0" prefWidth="106.0" text="Complete Sale" />
      <Button fx:id="removefromcartbtn" disable="true" layoutX="474.0" layoutY="338.0" mnemonicParsing="false" onAction="#removeAction" prefHeight="45.0" prefWidth="117.0" text="Remove From Cart" />
      <Button fx:id="addtocartbtn" disable="true" layoutX="299.0" layoutY="338.0" mnemonicParsing="false" onAction="#addtocartAction" prefHeight="45.0" prefWidth="88.0" text="Add to Cart" />
      <Button fx:id="resetbutton" layoutX="64.0" layoutY="338.0" mnemonicParsing="false" onAction="#resetAction" prefHeight="45.0" prefWidth="99.0" text="Reset Strore" />
      <ListView fx:id="storestock" layoutX="237.0" layoutY="74.0" prefHeight="245.0" prefWidth="200.0" />
   </children>
</AnchorPane>

控制器

package sample;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;

public class Controller {

    @FXML
    private static ListView<String> storestock;

    //here we are trying to populate the list view using model class that us ElectronicStroe.java
    //it does not populates the list also
    public void putData(){
        ElectronicStore e=new ElectronicStore();
        e.createStore();
        storestock =new ListView<String>();
        storestock.setItems(e.printStock());

    }

}

模型

package sample;
import java.util.ArrayList;

import javafx.collections.FXCollections;
import  javafx.collections.ObservableList;

public class ElectronicStore {
    public final int MAX_PRODUCTS = 10; //Maximum number of products the store can have
    private int curProducts;
    private String name;
    private Product[] stock; //Array to hold all products
    private double revenue;

    public ElectronicStore() {
        revenue = 0.0;
       // name = initName;
        stock = new Product[MAX_PRODUCTS];
        curProducts = 0;
    }

    public String getName() {
        return name;
    }

    //Adds a product and returns true if there is space in the array
    //Returns false otherwise
    public boolean addProduct(Product newProduct) {
        if (curProducts < MAX_PRODUCTS) {
            stock[curProducts] = newProduct;
            curProducts++;
            return true;
        }
        return false;
    }

    //Sells 'amount' of the product at 'index' in the stock array
    //Updates the revenue of the store
    //If no sale occurs, the revenue is not modified
    //If the index is invalid, the revenue is not modified
    public void sellProducts(int index, int amount) {
        if (index >= 0 && index < curProducts) {
            revenue += stock[index].sellUnits(amount);
        }
    }

    public double getRevenue() {
        return revenue;
    }

    //Prints the stock of the store
    public ObservableList<String> printStock() {
        ObservableList<String> x= FXCollections.observableArrayList();
        //ArrayList<String> x= new ArrayList<String>();
        for (int i = 0; i < curProducts; i++) {
            String s=(i + ". " + stock[i] + " (" + stock[i].getPrice() + " dollars each, " + stock[i].getStockQuantity() + " in stock, " + stock[i].getSoldQuantity() + " sold)");
            x.add(s);
        }
        return x;
    }

    public  ElectronicStore createStore() {
       // ElectronicStore store1 = new ElectronicStore();
        Desktop d1 = new Desktop(100, 10, 3.0, 16, false, 250, "Compact");
        Desktop d2 = new Desktop(200, 10, 4.0, 32, true, 500, "Server");
        Laptop l1 = new Laptop(150, 10, 2.5, 16, true, 250, 15);
        Laptop l2 = new Laptop(250, 10, 3.5, 24, true, 500, 16);
        Fridge f1 = new Fridge(500, 10, 250, "White", "Sub Zero", 15.5, false);
        Fridge f2 = new Fridge(750, 10, 125, "Stainless Steel", "Sub Zero", 23, true);
        ToasterOven t1 = new ToasterOven(25, 10, 50, "Black", "Danby", 8, false);
        ToasterOven t2 = new ToasterOven(75, 10, 50, "Silver", "Toasty", 12, true);
        this.addProduct(d1);
        this.addProduct(d2);
        this.addProduct(l1);
        this.addProduct(l2);
        this.addProduct(f1);
        this.addProduct(f2);
        this.addProduct(t1);
        this.addProduct(t2);
        return this;

    }
}

驱动程序代码

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import sample.*;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{

        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        Controller newCon=new Controller();
        newCon.putData();
        primaryStage.setTitle("Electronic Store Application - Watts up Electronics");
        primaryStage.setScene(new Scene(root, 800, 400));
        primaryStage.show();
        primaryStage.setResizable(false);
       

    }


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

标签: javalistviewjavafxmodel-view-controllerdata-binding

解决方案


推荐阅读