Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello So I am making a simple javafx table view and I am trying to put in inputs to add in the rows and an

Hello So I am making a simple javafx table view and I am trying to put in inputs to add in the rows and an add and delete button to add and delete rows but for some reason it runs but it has not been added into my table I've labeled the areas that include the buttons and inputs so it will be easy to find. (Edit: Im sorry but I've realised Im kinda dumb, I never put my hbox into my vbox.getChildren().addAll, so I have figured it out)

Main Class:

import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TableColumn; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javafx.scene.control.TableView; import java.lang.String; import javafx.scene.control.TextField; public class Main extends Application { Stage window; TableView table; TextField nameInput, priceInput, quantityInput; public static void main(String[] args){ launch(args); } @Override public void start(Stage primaryStage) throws Exception { window = primaryStage; window.setTitle("Program 3"); //Name Column TableColumn nameColumn = new TableColumn<>("Name"); nameColumn.setMinWidth(200); nameColumn.setCellValueFactory(new PropertyValueFactory<>("Name")); //Price Column TableColumn priceColumn = new TableColumn<>("Price"); priceColumn.setMinWidth(100); priceColumn.setCellValueFactory(new PropertyValueFactory<>("Price")); //Price Column TableColumn quantityColumn = new TableColumn<>("Quantity"); quantityColumn.setMinWidth(100); quantityColumn.setCellValueFactory(new PropertyValueFactory<>("Quantity")); //Name input nameInput = new TextField(); nameInput.setPromptText("Name"); nameInput.setMinWidth(100); //Price input priceInput = new TextField(); priceInput.setPromptText("Price"); //Quantity input quantityInput = new TextField(); //text input control quantityInput.setPromptText("Quantity"); //Button Button addButton = new Button("Add"); Button deleteButton = new Button("Delete"); //Hbox to layout the buttons HBox hbox = new HBox(); hbox.setPadding(new Insets(10)); hbox.setSpacing(10); //adding all the elements hbox.getChildren().addAll(nameInput, priceInput, quantityInput, addButton, deleteButton); table = new TableView<>(); table.setItems(getProduct()); table.getColumns().addAll(nameColumn, priceColumn, quantityColumn); VBox vbox = new VBox(); vbox.getChildren().addAll(table); Scene scene = new Scene(vbox); window.setScene(scene); window.show(); } //Get all of the products public ObservableList getProduct(){ ObservableList products = FXCollections.observableArrayList(); products.add(new Product("Thing 1", 859.00, 20)); products.add(new Product("Thing 2", 2.49, 198)); products.add(new Product("Thing 3", 99.00, 74)); products.add(new Product("Thing 4", 19.99, 12)); products.add(new Product("Thing 5", 1.49, 856)); return products; } }

Product Class:

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 void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions