努力从observbleArrayList获取内容以显示

时间:2018-12-02 13:44:20

标签: button javafx tableview

我正在尝试使用GUI中的TableView在JavaFx中制作比萨订购系统。我终于让它工作了,但是现在我在布局上有些挣扎。我无法获取我的observablearraylist的内容以仅显示按钮吗?

public class PizzaOrderingSystem extends Application {

    private final TableView<MenuItem> table = new TableView<>();
    private final ObservableList<MenuItem> ObservableList = 
            FXCollections.observableArrayList();

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

    @Override
    public void start(Stage stage) {

        stage.setTitle("Pizza Ordering System");
        stage.setWidth(600);
        stage.setHeight(600);

        setTableappearance();

        fillTableViewObservableListWithSampleMenuItem();
        table.setItems(ObservableList);

        //Name column
        TableColumn<MenuItem, String> nameColumn = new TableColumn<>("Name of item"); 
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));

        //Price column
        TableColumn<MenuItem, Double> priceColumn = new TableColumn<>("Price of item");
        priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));

        table.getColumns().addAll(nameColumn, priceColumn);

        addButtonToTable();

        Scene scene = new Scene(new Group(table));

        stage.setScene(scene);
        stage.show();
    }

    private void setTableappearance() {
        table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
        table.setPrefWidth(600);
        table.setPrefHeight(600);
    }

    private void fillTableViewObservableListWithSampleMenuItem() {

        ObservableList.addAll(new Pizza("Margherita", 50),
                new Pizza("Hawaii", 55),
                new Pizza("Marinara", 70),
                new Pizza("Meat Lovers", 70),
                new Pizza("Calazone", 60),

                new Burger("Burger", 60),
                new Burger("Cheeseburger", 65),
                new Burger("Baconburger", 65),

                new Soda("Coca cola", 25),
                new Soda("Coca cola light", 25),
                new Soda("Fanta", 25),
                new Soda("Faxe kondi", 25));  

    }

    private void addButtonToTable() {
        TableColumn<MenuItem, Void> button = new TableColumn("Select your items");

        Callback<TableColumn<MenuItem, Void>, TableCell<MenuItem, Void>> 
        cellFactory = new Callback<TableColumn<MenuItem, Void>, TableCell<MenuItem, Void>>() {

            @Override
            public TableCell<MenuItem, Void> call(final TableColumn<MenuItem, Void> param) {
                final TableCell<MenuItem, Void> cell = new TableCell<MenuItem, Void() { 

                    private final Button button = new Button("Select");

                    {
                        button.setOnAction((ActionEvent event) -> {
                            MenuItem menuItem = getTableView().getItems().get(getIndex());
                            System.out.println("selectedMenuItem: " + menuItem);
                        });
                    }

                    @Override
                    public void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(button);
                        }
                    }
                };
                return cell;
            }
        };

        button.setCellFactory(cellFactory);

        table.getColumns().add(button);
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

这是已解决的代码,请参见下面的图片。

public class PizzaOrderingSystem extends Application {

private final TableView<MenuItem> table = new TableView<>();
private final ObservableList<MenuItem> ObservableList = FXCollections.observableArrayList();


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

//Creating Stage and layout for Stage
@Override
public void start(Stage stage) {

    stage.setTitle("Pizza Ordering System");
    stage.setWidth(600);
    stage.setHeight(600);

//Assigning content to the tableView and setting appearance of tableView
    setTableappearance();

    fillTableViewObservableListWithSampleMenuItem();
    table.setItems(ObservableList);

        //Name column
        TableColumn<MenuItem, String> nameColumn = new TableColumn<>("Name of item");
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));

        //Price column
        TableColumn<MenuItem, Double> priceColumn = new TableColumn<>("Price of item");
        priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));

    table.getColumns().addAll(nameColumn, priceColumn);

    //Adding select buttons to the tableView
    addButtonToTable();

    //Adding borderPane, vBox and hBox to setup layout and easier control content in PizzaOrderingSystem
    BorderPane border = new BorderPane();

    //Making layout for vBox
    VBox vBox = new VBox();
    vBox.setVgrow (table, Priority.ALWAYS);
    vBox.getChildren().addAll(table);

    //Creaing a lable to sit in the hBox with the title of the Pizzaria + making layout for label
    Label label = new Label("Hungry Dog Pizzaria");
    label.setFont(Font.font ("Verdana",20));

    //Making Layout for hbox
    HBox hBox = new HBox();
    hBox.getChildren().add(label);
    hBox.setAlignment(Pos.CENTER);
    hBox.setMaxHeight(70);
    hBox.setPrefHeight(70);

    //Entering vBox and hBox to borderPane
    border.setTop(hBox);
    border.setCenter(vBox);

    //Attacing borderPane to scene
    Scene scene = new Scene(border);
    stage.setScene(scene);
    stage.show();
}

//setting appearance of tableView e.g. width and heigh of columns
private void setTableappearance() {
    table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    table.setPrefWidth(600);
    table.setPrefHeight(600);   
}

//Creating an ObservableArrayList with menu items containing attributes from the super class MenuItem 
private void fillTableViewObservableListWithSampleMenuItem() {

    ObservableList.addAll(new Pizza("Margherita", 50),
                          new Pizza("Hawaii", 55),
                          new Pizza("Marinara", 70),
                          new Pizza("Meat Lovers", 70),
                          new Pizza("Calazone", 60),

                          new Burger("Burger", 60),
                          new Burger("Cheeseburger", 65),
                          new Burger("Baconburger", 65),

                          new Soda("Coca cola", 25),
                          new Soda("Coca cola light", 25),
                          new Soda("Fanta", 25),
                          new Soda("Faxe kondi", 25));  

}


private void addButtonToTable() {
    TableColumn<MenuItem, Void> button = new TableColumn("Select your items");


    Callback<TableColumn<MenuItem, Void>, TableCell<MenuItem, Void>> cellFactory = new Callback<TableColumn<MenuItem, Void>, TableCell<MenuItem, Void>>() {

        @Override
        public TableCell<MenuItem, Void> call(final TableColumn<MenuItem, Void> param) {
        final TableCell<MenuItem, Void> cell = new TableCell<MenuItem, Void>() {

                private final Button button = new Button("Select");

                {
                    button.setOnAction((ActionEvent event) -> {
                    MenuItem menuItem = getTableView().getItems().get(getIndex());
                    System.out.println("Selected MenuItem: " + menuItem);
                    });
                }

                @Override
                public void updateItem(Void item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty) {
                        setGraphic(null);
                    } else {
                        setGraphic(button);
                    }
                }
            };
            return cell;
        }
    };

    button.setCellFactory(cellFactory);

    table.getColumns().add(button);


}
}

Picture of the code when it runs