import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TableEg extends Application
{
VBox layout;
TableView<Product> table;
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("Table of Fruits");
TableColumn<Product, String> namecolumn = new TableColumn<>("Name");
namecolumn.setMinWidth(30);
namecolumn.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<Product, Double> pricecolumn = new TableColumn<>("Price");
pricecolumn.setMinWidth(30);
namecolumn.setCellValueFactory(new PropertyValueFactory<>("price"));
TableColumn<Product, String> quantitycolumn = new TableColumn<>("Quantity");
quantitycolumn.setMinWidth(30);
namecolumn.setCellValueFactory(new PropertyValueFactory<>("quantity"));
table = new TableView<>();
table.setItems(getProduct());
table.getColumns().addAll(namecolumn, pricecolumn, quantitycolumn);
layout = new VBox();
Scene scene = new Scene(layout, 500, 500);
layout.getChildren().add(table);
primaryStage.setScene(scene);
primaryStage.show();
}
public ObservableList<Product> getProduct()
{
ObservableList<Product> product = FXCollections.observableArrayList();
product.add(new Product("Apple", 20, 5));
product.add(new Product("Banana", 3, 6));
product.add(new Product("WaterMelon", 60, 5));
product.add(new Product("Mango", 30, 10));
System.out.println(product);// this returns hashcode! Why??
return product;
}
}
Another Java file
public class Product
{
public String name;
public double price;
public 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(long price)
{
this.price = price;
}
public int getQuantity()
{
return quantity;
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
}
// 1) When I'm adding the contents to observable list, the hashcode of the class Product get add to the list. Why this is happening?? Please help!
2) The output list is created and the last argument ie quantity, is added to the name column name of the primarystage.