java中的Tableview错误

时间:2016-04-09 10:50:13

标签: java javafx tableview

import java.util.Arrays;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Testtt{

    static List<String> products_list;
    static List<String> quantity_list;
    static List<String> selling_price_list; 
    static List<String> revenue_list;
    static List<String> cost_price_list; 
    static List<String> profit_margin_list; 
    static List<String> profit_list;


    public void TableView(Stage window){

        BorderPane layout = new BorderPane();
        AnchorPane top = new AnchorPane();
        layout.setTop(top);
        Scene scene = new Scene(layout, 940, 630);

        Image Small_logo = new Image("file:Smaller_Logo.png"); 
        ImageView Small_logoView = new ImageView(Small_logo);
        Small_logoView.setFitWidth(100);
        Small_logoView.setPreserveRatio(true);

        Label title_message = new Label("Product sales");

        AnchorPane.setLeftAnchor(title_message, 5.0);
        AnchorPane.setRightAnchor(Small_logoView, 5.0);
        top.getChildren().addAll(title_message,Small_logoView);

        VBox center = new VBox(10);
        center.setPadding(new Insets(20,10,0,10));

        products_list = Arrays.asList("Coffee", "Tea");
        quantity_list = Arrays.asList("5", "10");
        selling_price_list = Arrays.asList("10", "4");
        revenue_list = Arrays.asList("100", "120");
        cost_price_list = Arrays.asList("5", "2");
        profit_margin_list = Arrays.asList("50%", "5%");
        profit_list = Arrays.asList("25", "20");

        TableColumn<getting_data,String> products = new TableColumn<getting_data,String>("Product name");
        products.setMinWidth(180);
        products.setCellValueFactory(new PropertyValueFactory<>("product"));

        TableColumn<getting_data,String> quantity = new TableColumn<getting_data,String>("Quantity");
        quantity.setMinWidth(120);
        quantity.setCellValueFactory(new PropertyValueFactory<>("quantity"));

        TableColumn<getting_data,String> Selling_price_column = new TableColumn<getting_data,String>("Selling price");
        Selling_price_column.setMinWidth(160);
        Selling_price_column.setCellValueFactory(new PropertyValueFactory<>("Selling_price"));

        TableColumn<getting_data,String> revenue_column = new TableColumn<getting_data,String>("Revenue");
        revenue_column.setMinWidth(120);
        revenue_column.setCellValueFactory(new PropertyValueFactory<>("revenue"));

        TableColumn<getting_data,String> cost_price_column = new TableColumn<getting_data,String>("Cost");
        cost_price_column.setMinWidth(80);
        cost_price_column.setCellValueFactory(new PropertyValueFactory<>("cost"));

        TableColumn<getting_data,String> profit_column = new TableColumn<getting_data,String>("Profit");
        profit_column.setMinWidth(90);
        profit_column.setCellValueFactory(new PropertyValueFactory<>("profit"));

        TableColumn<getting_data,String> profit_margin_column = new TableColumn<getting_data,String>("Profit margin");
        profit_margin_column.setMinWidth(170);
        profit_margin_column.setCellValueFactory(new PropertyValueFactory<>("profit_margin"));

        TableView<getting_data> Table = new TableView<>();
        Table.setItems(load_products());
        Table.getColumns().addAll(products, quantity, Selling_price_column,revenue_column,cost_price_column,profit_column,profit_margin_column);

        center.getChildren().add(Table);
        layout.setCenter(center);

        HBox bottom_layout = new HBox(10);
        bottom_layout.setAlignment(Pos.BOTTOM_RIGHT);

        Button back = new Button("Go back to home screen");

        bottom_layout.getChildren().add(back);
        layout.setBottom(bottom_layout);

        scene.getStylesheets().add("Admin_style.css");

        window.setScene(scene);
    }

    public ObservableList<getting_data> load_products(){
        ObservableList<getting_data> products_observable_list = FXCollections.observableArrayList();
        for(int i= 0;i<2;i++){
            products_observable_list.add(new getting_data(products_list.get(i),quantity_list.get(i),selling_price_list.get(i),revenue_list.get(i),cost_price_list.get(i),profit_list.get(i),profit_margin_list.get(i)));}
        return products_observable_list;}

    public class getting_data{

        private String product;
        private String quantity;
        private String Selling_price;
        private String revenue;
        private String cost;
        private String profit;
        private String profit_margin;

        public getting_data(String product,String quantity,String Selling_price,String revenue,String cost,String profit,String profit_margin){
            this.quantity = quantity;
            this.product = product;
            this.Selling_price = Selling_price;
            this.revenue = revenue;
            this.cost = cost;
            this.profit = profit;
            this.profit_margin = profit_margin;}

        public String getProduct(){return product;}
        public void setProduct(String product){this.product=product;}

        public String getQuantity(){return quantity;}
        public void setQuantity(String quantity){this.quantity=quantity;}

        public String getSelling_price(){return Selling_price;}
        public void setSelling_price(String Selling_price){this.Selling_price=Selling_price;}

        public String getRevenue(){return revenue;}
        public void setRevenue(String revenue){this.revenue=revenue;}

        public String getCost(){return cost;}
        public void setCost(String cost){this.cost=cost;}

        public String getProfit(){return profit;}
        public void setProfit(String profit){this.profit=profit;}

        public String getMargin(){return profit_margin;}
        public void setMargin(String profit_margin){this.profit_margin=profit_margin;}

    }
    public static void passer(Stage window){
        Testtt function = new Testtt();
        function.TableView(window);
    }
}

此代码不显示利润率列的值,不太清楚为什么。我通过将利润率字符串放在其他位置并显示正确的值来检查字符串是否存在问题。我认为这可能是我的吸气剂和制定者的一个错误,但我似乎无法找到它。

感谢您的帮助! :)

1 个答案:

答案 0 :(得分:0)

PropertyValueFactory使用这些方法来访问属性/ getter,而不是访问此getter检索的字段。 (应该怎么做?getter不需要在字段中存储返回值。)

您必须为bean使用正确的名称。由于字段的名称为profit_margin,但 getter / setter 的名称为getMargin / setMargin,因此应为

new PropertyValueFactory<>("margin")

而不是

new PropertyValueFactory<>("profit_margin")