Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.application.Platform;
  3. import javafx.collections.FXCollections;
  4. import javafx.collections.ObservableList;
  5. import javafx.geometry.Insets;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.*;
  8. import javafx.scene.control.cell.PropertyValueFactory;
  9. import javafx.scene.layout.*;
  10. import javafx.stage.Stage;
  11.  
  12. public class Main extends Application {
  13.  
  14. Stage window;
  15. TableView<Product> table;
  16. TextField nameInput, priceInput, quantityInput;
  17.  
  18. public static void main(String[] args) {
  19. launch(args);
  20. }
  21.  
  22. @Override
  23. public void start(Stage primaryStage) {
  24. window = primaryStage;
  25. window.setTitle("Austin");
  26.  
  27. //Name column
  28. TableColumn<Product, String> nameColumn = new TableColumn<>("Name");
  29. nameColumn.setMinWidth(200);
  30. nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
  31.  
  32. //Price column
  33. TableColumn<Product, Double> priceColumn = new TableColumn<>("Price");
  34. priceColumn.setMinWidth(100);
  35. priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));
  36.  
  37. //quantity column
  38. TableColumn<Product, Integer> quantityColumn = new TableColumn<>("Quantity");
  39. quantityColumn.setMinWidth(100);
  40. quantityColumn.setCellValueFactory(new PropertyValueFactory<>("quantity"));
  41.  
  42.  
  43. //Name Input
  44. nameInput = new TextField();
  45. nameInput.setPromptText("Name");
  46.  
  47. //Price Input
  48. priceInput = new TextField();
  49. priceInput.setPromptText("Price");
  50.  
  51. //Quantity Input
  52. quantityInput = new TextField();
  53. quantityInput.setPromptText("Quantity");
  54.  
  55. //Button
  56. Button addButton = new Button("Add");
  57. Button deleteButton = new Button("Delete Item");
  58.  
  59. //Button Events
  60. addButton.setOnAction(event -> addButtonClicked());
  61. deleteButton.setOnAction(event -> deleteButtonClicked());
  62.  
  63. HBox hBox = new HBox();
  64. hBox.setPadding(new Insets(10, 10, 10, 10));
  65. hBox.setSpacing(10);
  66. hBox.getChildren().addAll(nameInput, priceInput, quantityInput, addButton, deleteButton);
  67.  
  68. table = new TableView<>();
  69. table.setItems(getProduct());
  70. table.getColumns().addAll(nameColumn, priceColumn, quantityColumn);
  71.  
  72. //Layout
  73. VBox vBox = new VBox();
  74. vBox.getChildren().addAll(table, hBox);
  75.  
  76. Scene scene = new Scene(vBox);
  77. window.setScene(scene);
  78.  
  79. window.show();
  80. }
  81.  
  82. //Add button clicked
  83. public void addButtonClicked(){
  84. Product product = new Product();
  85. product.setName(nameInput.getText());
  86. product.setPrice(Double.parseDouble(priceInput.getText()));
  87. product.setQuantity(Integer.parseInt(quantityInput.getText()));
  88. table.getItems().add(product);
  89. nameInput.clear();
  90. priceInput.clear();
  91. quantityInput.clear();
  92. }
  93.  
  94. //Delete button clicked
  95. public void deleteButtonClicked(){
  96. ObservableList<Product> productSelected, allProducts;
  97. allProducts = table.getItems();
  98. productSelected = table.getSelectionModel().getSelectedItems();
  99.  
  100. productSelected.forEach(allProducts::remove);
  101. }
  102.  
  103. //Get all of the products
  104. public ObservableList<Product> getProduct(){
  105. ObservableList<Product> products = FXCollections.observableArrayList();
  106. products.add(new Product("laptop", 859, 20));
  107. products.add(new Product("fan", 59, 10));
  108. products.add(new Product("chair", 89, 2));
  109. products.add(new Product("movies", 19, 12));
  110. products.add(new Product("Mountain Dew", 8, 100));
  111. return products;
  112. }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement