JavaFX Tableview持有两种对象

时间:2017-05-06 11:22:12

标签: java javafx tableview fxml classcastexception

我目前正在编写一个使用JavaFX和FXML的程序。我当前的问题是我有一个FXMLTableView,它应该显示两个不同对象类StockorderArticle的特征,即文章的名称,ID和价格以及数量那篇文章有库存(在stockorder中定义)。我目前有以下代码:

Stockorder:

public class Stockorder {

    private SimpleStringProperty deliveryState;
    private SimpleStringProperty stockOrderId;
    private SimpleStringProperty orderdate;
    private SimpleStringProperty deliverydate;
    private SimpleStringProperty count;
    private SimpleStringProperty itemId;
    private SimpleStringProperty itemName;

    public Stockorder(String deliveryState, String stockOrderId, String orderdate, String deliverydate, String count, String itemId, String itemName) {
        this.deliveryState = new SimpleStringProperty(deliveryState);
        this.stockOrderId = new SimpleStringProperty(stockOrderId);
        this.orderdate = new SimpleStringProperty(orderdate);
        this.deliverydate = new SimpleStringProperty(deliverydate);
        this.count = new SimpleStringProperty(count);
        this.itemId = new SimpleStringProperty(itemId);
        this.itemName = new SimpleStringProperty(itemName);
    }

    public boolean getDeliveryState() {
        return Boolean.valueOf(deliveryState.get());
    }

    public StringProperty getDeliveryStateProperty() {
        return deliveryState;
    }

    public int getStockOrderId() {
        return Integer.parseInt(stockOrderId.get());
    }

    public StringProperty getStockOrderIdProperty() {
        return stockOrderId;
    }

    public String getOrderdate() {
        return orderdate.get();
    }

    public StringProperty getOrderdateProperty() {
        return orderdate;
    }

    public String getDeliverydate() {
        return deliverydate.get();
    }

    public StringProperty getDeliverydateProperty() {
        return deliverydate;
    }

    public int getCount() {
        return Integer.parseInt(count.get());
    }

    public StringProperty getCountProperty() {
        return count;
    }

    public int getItemId() {
        return Integer.parseInt(itemId.get());
    }

    public StringProperty getItemIdProperty() {
        return itemId;
    }

    public String getItemName() {
        return itemName.get();
    }

    public StringProperty getItemNameProperty() {
        return itemName;
    }
}

文章:

public class Article {
    private final SimpleStringProperty articleNr;
    private final SimpleStringProperty name;
    private final SimpleStringProperty price;

    public Article(String articleNr, String name, String price) {
        this.articleNr = new SimpleStringProperty(articleNr);
        this.name = new SimpleStringProperty(name);
        this.price = new SimpleStringProperty(price);
    }

    public int getArticleNr() {
        return Integer.parseInt(articleNr.get());
    }

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

    public double getPrice() {
        return Double.parseDouble(price.get());
    }

    public StringProperty getArticleNrProperty(){
        return articleNr;
    }

    public StringProperty getNameProperty(){
        return name;
    }

    public StringProperty getPriceProperty(){
        return price;
    }
}

EditArticleOverviewControl(FXMLController)

public class EditArticlesOverviewController implements Initializable {

    public EditArticlesOverviewController() {
        System.out.println("EditArticleOverviewController started");

    }

    private ApplicationControl applicationControl;
    private EditOrderController editOrderController = new EditOrderController();
    private String stockOrderURL = "stockorders";
    private HttpClient client = new HttpClient();

    @FXML
    TableView<Article> tableView;

    @FXML
    TableColumn<Article, String> articleNrColumn;

    @FXML
    TableColumn<Article, String> nameColumn;

    @FXML
    TableColumn<Article, String> priceColumn;

    @FXML
    TableColumn<Stockorder, String> stockColumn;

    @FXML
    TableColumn<Stockorder, String> minStockColumn;

    @FXML
    TableColumn<Stockorder, String> addColumn;

    @FXML
    Button cancelButton;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        articleNrColumn.setCellValueFactory(cellData -> cellData.getValue().getArticleNrProperty());
        nameColumn.setCellValueFactory(cellData -> cellData.getValue().getNameProperty());
        priceColumn.setCellValueFactory(cellData -> cellData.getValue().getPriceProperty());
        stockColumn.setCellValueFactory(cellData -> cellData.getValue().getCountProperty());
        minStockColumn.setCellValueFactory(cellData -> cellData.getValue().getDeliveryStateProperty());
        addColumn.setCellValueFactory(cellData -> cellData.getValue().getCountProperty());
    }

    public void setApplicationControl(ApplicationControl applicationControl) {
        this.applicationControl = applicationControl;
    }

    public void setEntriesInTableView(ObservableList articlesList) {
        tableView.setItems(articlesList);
    }

    @FXML
    public void goBack() {
        System.out.println("Kein Artikel bearbeitet");
        Stage stage = (Stage) cancelButton.getScene().getWindow();
        stage.close();
        //EditOrder wieder starten vom selben Order wie vorher!!
        try {
            applicationControl.openEditOrder(applicationControl.order.getOrderNr());
        } catch (Exception ex) {
            Logger.getLogger(OrdersOverviewController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

现在,每当我尝试运行程序时,都会收到错误:

java.lang.ClassCastException: client.Article cannot be cast to client.Stockorder

在我为cellValueFactory设置stockOrder的行。但我无法理解原因,因为该列包含StockordercellValueFactory使用Stockorder类的方法。

1 个答案:

答案 0 :(得分:1)

在你的代码中,

TableColumn<Stockorder, String> stockColumn;

这意味着stockColumn会尝试将商品作为Stockorder TableColumn<S, T>的S影响setCellValueFactory(Callback<TableColumn.CellDataFeatures<S,T>,ObservableValue<T>> value)的S并返回TableColumn.CellDataFeatures<S,T>::getValue()的S类型。

但实际上,TableView中的项目不是Stockorder的实例,因此在运行时抛出ClassCastException。所有S类TableView<S>TableColumn<S,T>必须相同。

在这种情况下,要在TableView中显示它们,需要创建一个数据模型类来使用它。例如:

class RowData {
    private Stockorder stockorder;
    private Article article;
    public RowData(Arthicle article, Stockorder stockorder) {
        this.article = article;
        this.stockorder = stockorder;
    }
    public Stockorder getStockorder(){ return stockorder; }
    public Article getArticle(){ return arthicle; }
}