Skip to content

Commit 7212d1e

Browse files
committed
More TableView
1 parent 4f03f2e commit 7212d1e

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
public class Product {
2+
3+
private String name;
4+
private double price;
5+
private int quantity;
6+
7+
public Product(){
8+
this.name = "";
9+
this.price = 0;
10+
this.quantity = 0;
11+
}
12+
13+
public Product(String name, double price, int quantity){
14+
this.name = name;
15+
this.price = price;
16+
this.quantity = quantity;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
public double getPrice() {
28+
return price;
29+
}
30+
31+
public void setPrice(double price) {
32+
this.price = price;
33+
}
34+
35+
public int getQuantity() {
36+
return quantity;
37+
}
38+
39+
public void setQuantity(int quantity) {
40+
this.quantity = quantity;
41+
}
42+
43+
}

0 commit comments

Comments
 (0)