TableView无法正确显示项目

时间:2015-11-18 14:45:45

标签: java javafx tableview tablecolumn

我希望我的TableView与我的三个TableColumns一起显示一些产品。 添加新产品后,它们将保存在数据容器中,但不会显示在“表视图”

import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.util.Callback;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.cell.TextFieldTableCell;
public class ViewShop extends BorderPane {

    private Button add;
    private Button remove;
    private TextField name;
    private TextField price;
    private TextField count;
    private VBox vbox;
    private HBox hbox;
    public TableView<fpt.com.Product> tv;
    private TableColumn<fpt.com.Product, Number> sprice;
    private TableColumn<fpt.com.Product, String> sname;
    private TableColumn <fpt.com.Product, Number>scount;
    private Label namelabel;
    private Label pricelabel;
    private Label countlabel;

    private ListView<fpt.com.Product> list; 

    public ViewShop()
    {

        remove=new Button("Remove");
        add=new Button("Add");
        name=new TextField();
        price=new TextField();
        count=new TextField();
        tv=new TableView<fpt.com.Product>();
        sprice=new TableColumn<fpt.com.Product, Number>("Preis");
        sname=new TableColumn<fpt.com.Product, String>("Name");
        scount=new TableColumn<fpt.com.Product, Number>("Anzahl");
        namelabel=new Label("Name");
        pricelabel=new Label("Preis");
        countlabel=new Label("Anzahl");
        list = new ListView<fpt.com.Product>();
        hbox=new HBox();
        vbox=new VBox();
        tv.getColumns().addAll(sname,sprice,scount);
        tv.setEditable(true);
        tv.getColumns().get(0).getCellData(0);
        tv.setItems(list.getItems());
        sname.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
        sprice.setCellValueFactory(cellData -> cellData.getValue().priceProperty());
        scount.setCellValueFactory(cellData -> cellData.getValue().quantityProperty());
        tv.setItems(list.getItems());
        hbox.getChildren().addAll(remove, add);
        vbox.getChildren().addAll(namelabel,name,pricelabel ,price, countlabel , count, hbox);
        this.setRight(vbox);
        this.setLeft(tv);
    }
    public void addEventHandleradd(EventHandler<ActionEvent> eventHandler) {
        this.add.addEventHandler(ActionEvent.ACTION, eventHandler);
    }
    public void addEventHandlerremove(EventHandler<ActionEvent> eventHandler) {
        this.remove.addEventHandler(ActionEvent.ACTION, eventHandler);
    }
    public String giveName()
    {
        return name.getText();
    }
    public String givePrice()
    {
        return price.getText();
    }
    public String giveCount()
    {
        return count.getText();
    }
    public ListView<fpt.com.Product> getList()
    {
        return list;
    }


}

Class ControllerShop

import javafx.event.ActionEvent;
import javafx.event.EventHandler;

public class ControllerShop {

    ModelShop model;
    ViewShop view;

    public void link(ModelShop model, ViewShop view)
    {
        this.model=model;
        this.view=view;
        model.setProductList();
        view.getList().setItems(model);
        view.addEventHandleradd(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                model.doAdd(model.size(), new Product(view.giveName(), Integer.parseInt(view.giveCount()), Double.parseDouble(view.givePrice())));
                System.out.println(view.getList().getItems().size());
                System.out.println(model.size());
            }
        });
        view.addEventHandlerremove(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                model.remove(model.findProductByName(view.giveName()));
                }
        });

    }

}

类产品

import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.InvalidationListener;
import javafx.beans.property.DoubleProperty;

public class Product implements fpt.com.Product {

    Long id;
    SimpleStringProperty name;
    SimpleIntegerProperty quantity;
    SimpleDoubleProperty price;

    public Product(String name, int quantity, double price)
    {
        this.name=new SimpleStringProperty(name);
        this.quantity=new SimpleIntegerProperty(quantity);
        this.price=new SimpleDoubleProperty(price);
    }




    @Override
    public long getId() {
        return id;
    }

    @Override
    public void setId(long id) {
        this.id=id;

    }

    @Override
    public double getPrice() {
        return this.price.get();
    }

    @Override
    public void setPrice(double price) {
        this.price.set(price);
    }

    @Override
    public int getQuantity() {
        return this.quantity.get();
    }

    @Override
    public void setQuantity(int quantity) {
        this.quantity.set(quantity);
    }

    @Override
    public String getName() {
        return this.name.get();
    }

    @Override
    public void setName(String name) {
        this.name.set(name);
    }

    @Override
    public StringProperty nameProperty() {
        return name;
    }

    @Override
    public DoubleProperty priceProperty() {
        return price;
    }

    @Override
    public IntegerProperty quantityProperty() {
        return quantity;
    }

}

Class Main

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application{

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

    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        ModelShop model=new ModelShop();
        ViewShop view=new ViewShop();
        ControllerShop controller=new ControllerShop();
        controller.link(model, view);
        primaryStage.setScene(new Scene(view, 400, 400));
        primaryStage.show();

    }

}

1 个答案:

答案 0 :(得分:0)

ViewShop构造函数中,您将表格的项目列表设置为ListView的项目列表:

tv.setItems(list.getItems());

然后,稍后,在控制器的构造函数中,将ListView的项目列表更改为模型中的列表:

view.getList().setItems(model);

但是,此时不更改表的项目列表。所以现在ListView的项目列表是模型,但表格的项目列表是ListView中的原始(默认)项目列表。因此,当您向模型添加内容时,表格将不会显示这些项目。

您需要在控制器构造函数中将表的项列表设置为模型:

view.getTable().setItems(model);

(在视图中定义了明显的getTable()方法),或者您可以通过替换

将表的项绑定到列表的项目
tv.setItems(list.getItems());

tv.itemsProperty().bind(list.itemsProperty());

在视图构造函数中。