0
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.

Rana Depto
  • 721
  • 3
  • 11
  • 31
  • 1
    It doesn't return the hashCode. It prints the class name followed by the hashCode. What else do you think it should print? Have you told Java anywhere how a Product should be printed? How could it guess? Hint: read the javadoc of Object.toString(). – JB Nizet May 03 '16 at 17:10
  • 2) is just because of the obvious copy-and-paste error you made: you do `nameColumn.setCellValueFactory(..)` three times instead of `nameColumn.setCellValueFactory(...)`, `priceColumn.setCellValueFactory(...)`, etc. – James_D May 03 '16 at 17:10
  • 1
    Please note in future that it would be much easier to help you with a [mcve] - not 150 lines of mostly-irrelevant code. – Jon Skeet May 03 '16 at 17:11
  • add method adds the hashcode to the list. So how the getItem() method got the elements?? – Kushal Maharana May 03 '16 at 17:21

0 Answers0