|
| 1 | +import javafx.application.Application; |
| 2 | +import javafx.collections.FXCollections; |
| 3 | +import javafx.collections.ObservableList; |
| 4 | +import javafx.geometry.Insets; |
| 5 | +import javafx.scene.Scene; |
| 6 | +import javafx.scene.control.Button; |
| 7 | +import javafx.scene.control.TableColumn; |
| 8 | +import javafx.scene.control.TableView; |
| 9 | +import javafx.scene.control.TextField; |
| 10 | +import javafx.scene.control.cell.PropertyValueFactory; |
| 11 | +import javafx.scene.layout.HBox; |
| 12 | +import javafx.scene.layout.VBox; |
| 13 | +import javafx.stage.Stage; |
| 14 | + |
| 15 | +public class Main extends Application { |
| 16 | + |
| 17 | + Stage window; |
| 18 | + TableView<Product> table; |
| 19 | + TextField nameInput, priceInput, quantityInput; |
| 20 | + |
| 21 | + public static void main(String[] args) { |
| 22 | + launch(args); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public void start(Stage primaryStage) throws Exception { |
| 27 | + window = primaryStage; |
| 28 | + window.setTitle("thenewboston - JavaFX"); |
| 29 | + |
| 30 | + //Name column |
| 31 | + TableColumn<Product, String> nameColumn = new TableColumn<>("Name"); |
| 32 | + nameColumn.setMinWidth(200); |
| 33 | + nameColumn.setCellValueFactory(new PropertyValueFactory<>("name")); |
| 34 | + |
| 35 | + //Price column |
| 36 | + TableColumn<Product, Double> priceColumn = new TableColumn<>("Price"); |
| 37 | + priceColumn.setMinWidth(100); |
| 38 | + priceColumn.setCellValueFactory(new PropertyValueFactory<>("price")); |
| 39 | + |
| 40 | + //Quantity column |
| 41 | + TableColumn<Product, String> quantityColumn = new TableColumn<>("Quantity"); |
| 42 | + quantityColumn.setMinWidth(100); |
| 43 | + quantityColumn.setCellValueFactory(new PropertyValueFactory<>("quantity")); |
| 44 | + |
| 45 | + //Name input |
| 46 | + nameInput = new TextField(); |
| 47 | + nameInput.setPromptText("Name"); |
| 48 | + nameInput.setMinWidth(100); |
| 49 | + |
| 50 | + //Price input |
| 51 | + priceInput = new TextField(); |
| 52 | + priceInput.setPromptText("Price"); |
| 53 | + |
| 54 | + //Quantity input |
| 55 | + quantityInput = new TextField(); |
| 56 | + quantityInput.setPromptText("Quantity"); |
| 57 | + |
| 58 | + //Button |
| 59 | + Button addButton = new Button("Add"); |
| 60 | + addButton.setOnAction(e -> addButtonClicked()); |
| 61 | + Button deleteButton = new Button("Delete"); |
| 62 | + deleteButton.setOnAction(e -> deleteButtonClicked()); |
| 63 | + |
| 64 | + HBox hBox = new HBox(); |
| 65 | + hBox.setPadding(new Insets(10,10,10,10)); |
| 66 | + hBox.setSpacing(10); |
| 67 | + hBox.getChildren().addAll(nameInput, priceInput, quantityInput, addButton, deleteButton); |
| 68 | + |
| 69 | + table = new TableView<>(); |
| 70 | + table.setItems(getProduct()); |
| 71 | + table.getColumns().addAll(nameColumn, priceColumn, quantityColumn); |
| 72 | + |
| 73 | + VBox vBox = new VBox(); |
| 74 | + vBox.getChildren().addAll(table, hBox); |
| 75 | + |
| 76 | + Scene scene = new Scene(vBox); |
| 77 | + window.setScene(scene); |
| 78 | + window.show(); |
| 79 | + } |
| 80 | + |
| 81 | + //Add button clicked |
| 82 | + public void addButtonClicked(){ |
| 83 | + Product product = new Product(); |
| 84 | + product.setName(nameInput.getText()); |
| 85 | + product.setPrice(Double.parseDouble(priceInput.getText())); |
| 86 | + product.setQuantity(Integer.parseInt(quantityInput.getText())); |
| 87 | + table.getItems().add(product); |
| 88 | + nameInput.clear(); |
| 89 | + priceInput.clear(); |
| 90 | + quantityInput.clear(); |
| 91 | + } |
| 92 | + |
| 93 | + //Delete button clicked |
| 94 | + public void deleteButtonClicked(){ |
| 95 | + ObservableList<Product> productSelected, allProducts; |
| 96 | + allProducts = table.getItems(); |
| 97 | + productSelected = table.getSelectionModel().getSelectedItems(); |
| 98 | + |
| 99 | + productSelected.forEach(allProducts::remove); |
| 100 | + } |
| 101 | + |
| 102 | + //Get all of the products |
| 103 | + public ObservableList<Product> getProduct(){ |
| 104 | + ObservableList<Product> products = FXCollections.observableArrayList(); |
| 105 | + products.add(new Product("Laptop", 859.00, 20)); |
| 106 | + products.add(new Product("Bouncy Ball", 2.49, 198)); |
| 107 | + products.add(new Product("Toilet", 99.00, 74)); |
| 108 | + products.add(new Product("The Notebook DVD", 19.99, 12)); |
| 109 | + products.add(new Product("Corn", 1.49, 856)); |
| 110 | + return products; |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | +} |