Skip to content

Commit 4f03f2e

Browse files
committed
TableView
1 parent e8e7605 commit 4f03f2e

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

JavaFX/018_tableView/Main.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
20+
public static void main(String[] args) {
21+
launch(args);
22+
}
23+
24+
@Override
25+
public void start(Stage primaryStage) throws Exception {
26+
window = primaryStage;
27+
window.setTitle("thenewboston - JavaFX");
28+
29+
//Name column
30+
TableColumn<Product, String> nameColumn = new TableColumn<>("Name");
31+
nameColumn.setMinWidth(200);
32+
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
33+
34+
//Price column
35+
TableColumn<Product, Double> priceColumn = new TableColumn<>("Price");
36+
priceColumn.setMinWidth(100);
37+
priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));
38+
39+
//Quantity column
40+
TableColumn<Product, String> quantityColumn = new TableColumn<>("Quantity");
41+
quantityColumn.setMinWidth(100);
42+
quantityColumn.setCellValueFactory(new PropertyValueFactory<>("quantity"));
43+
44+
table = new TableView<>();
45+
table.setItems(getProduct());
46+
table.getColumns().addAll(nameColumn, priceColumn, quantityColumn);
47+
48+
VBox vBox = new VBox();
49+
vBox.getChildren().addAll(table);
50+
51+
Scene scene = new Scene(vBox);
52+
window.setScene(scene);
53+
window.show();
54+
}
55+
56+
//Get all of the products
57+
public ObservableList<Product> getProduct(){
58+
ObservableList<Product> products = FXCollections.observableArrayList();
59+
products.add(new Product("Laptop", 859.00, 20));
60+
products.add(new Product("Bouncy Ball", 2.49, 198));
61+
products.add(new Product("Toilet", 99.00, 74));
62+
products.add(new Product("The Notebook DVD", 19.99, 12));
63+
products.add(new Product("Corn", 1.49, 856));
64+
return products;
65+
}
66+
67+
68+
}

JavaFX/018_tableView/Product.java

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)