Last active
August 29, 2015 14:24
-
-
Save ahn94/7c2fe72720f4003ac801 to your computer and use it in GitHub Desktop.
Good example:observalbe listtableviewsimle database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.application.Platform; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.geometry.Insets; | |
import javafx.scene.Scene; | |
import javafx.scene.control.*; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.scene.layout.*; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
Stage window; | |
TableView<Product> table; | |
TextField nameInput, priceInput, quantityInput; | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage primaryStage) { | |
window = primaryStage; | |
window.setTitle("Austin"); | |
//Name column | |
TableColumn<Product, String> nameColumn = new TableColumn<>("Name"); | |
nameColumn.setMinWidth(200); | |
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name")); | |
//Price column | |
TableColumn<Product, Double> priceColumn = new TableColumn<>("Price"); | |
priceColumn.setMinWidth(100); | |
priceColumn.setCellValueFactory(new PropertyValueFactory<>("price")); | |
//quantity column | |
TableColumn<Product, Integer> quantityColumn = new TableColumn<>("Quantity"); | |
quantityColumn.setMinWidth(100); | |
quantityColumn.setCellValueFactory(new PropertyValueFactory<>("quantity")); | |
//Name Input | |
nameInput = new TextField(); | |
nameInput.setPromptText("Name"); | |
//Price Input | |
priceInput = new TextField(); | |
priceInput.setPromptText("Price"); | |
//Quantity Input | |
quantityInput = new TextField(); | |
quantityInput.setPromptText("Quantity"); | |
//Button | |
Button addButton = new Button("Add"); | |
Button deleteButton = new Button("Delete Item"); | |
//Button Events | |
addButton.setOnAction(event -> addButtonClicked()); | |
deleteButton.setOnAction(event -> deleteButtonClicked()); | |
HBox hBox = new HBox(); | |
hBox.setPadding(new Insets(10, 10, 10, 10)); | |
hBox.setSpacing(10); | |
hBox.getChildren().addAll(nameInput, priceInput, quantityInput, addButton, deleteButton); | |
table = new TableView<>(); | |
table.setItems(getProduct()); | |
table.getColumns().addAll(nameColumn, priceColumn, quantityColumn); | |
//Layout | |
VBox vBox = new VBox(); | |
vBox.getChildren().addAll(table, hBox); | |
Scene scene = new Scene(vBox); | |
window.setScene(scene); | |
window.show(); | |
} | |
//Add button clicked | |
public void addButtonClicked(){ | |
Product product = new Product(); | |
product.setName(nameInput.getText()); | |
product.setPrice(Double.parseDouble(priceInput.getText())); | |
product.setQuantity(Integer.parseInt(quantityInput.getText())); | |
table.getItems().add(product); | |
nameInput.clear(); | |
priceInput.clear(); | |
quantityInput.clear(); | |
} | |
//Delete button clicked | |
public void deleteButtonClicked(){ | |
ObservableList<Product> productSelected, allProducts; | |
allProducts = table.getItems(); | |
productSelected = table.getSelectionModel().getSelectedItems(); | |
productSelected.forEach(allProducts::remove); | |
} | |
//Get all of the products | |
public ObservableList<Product> getProduct(){ | |
ObservableList<Product> products = FXCollections.observableArrayList(); | |
products.add(new Product("laptop", 859, 20)); | |
products.add(new Product("fan", 59, 10)); | |
products.add(new Product("chair", 89, 2)); | |
products.add(new Product("movies", 19, 12)); | |
products.add(new Product("Mountain Dew", 8, 100)); | |
return products; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Product { | |
private String name; | |
private double price; | |
private int quantity; | |
public Product(){ | |
this.name = ""; | |
this.price = 0; | |
this.quantity = 0; | |
} | |
public Product(String name, double price, int quantity){ | |
this.name = name; | |
this.price = price; | |
this.quantity = quantity; | |
} | |
public String getName() { | |
return name; | |
} | |
public double getPrice() { | |
return price; | |
} | |
public int getQuantity() { | |
return quantity; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public void setPrice(double price) { | |
this.price = price; | |
} | |
public void setQuantity(int quantity) { | |
this.quantity = quantity; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment