Link to home
Start Free TrialLog in
Avatar of AttilaB
AttilaB

asked on

JavaFX TableView not displaying correctly

I am trying to learn JavaFX, and one of the very important controls, TableView is not displaying correctly, even though it compiles and runs without errors:
User generated image
I am expecting the DATA to show up in the rows and columns of the table.


My code:
1. Main class: (Also setting values for the properties of Product class here)
package tableviewdemo;

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.VBox;
import javafx.stage.Stage;


public class TableViewDemo extends Application {
    
    // You need to make a list of objects for the tables, and then convert them into ObservableList
    // (Objects -> ObservableList -> Table)
    
  
    public void start(Stage primaryStage) {
        
        // This is the entire table:
        TableView table;
        
        // (At least one column defined)
        // Name column:
        TableColumn<Product, String> nameColumn = new TableColumn<>("Name");  // Name column header is passed in constructor
        nameColumn.setMinWidth(200);  // If not specified all the columns are bunched together
        nameColumn.setCellValueFactory(new PropertyValueFactory<Product, String>("name"));  
                // It has to be the EXACT NAME as your property name
                // passed to PropertyValueFactory() constructor!
        
        
        // Price column:
        TableColumn<Product, Double> priceColumn = new TableColumn<>("Price");  // Name column header is passed in constructor
        nameColumn.setMinWidth(100);  // If not specified all the columns are bunched together
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("price"));
        
        
        // Quantity Column:
        TableColumn<Product, Integer> quantityColumn = new TableColumn<>("Quantity");  // Name column header is passed in constructor
        nameColumn.setMinWidth(100);  // If not specified all the columns are bunched together
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("quantity"));   
            // Type is implied, you don't have to indicate 
            
            
            
        // Setting the columns inside the table, load all the data:
        table = new TableView();
        table.setItems(getProducts());  // It will load the data as ObservableList
        table.getColumns().addAll(nameColumn, priceColumn, quantityColumn);  // add all the columns to table
       
       
        VBox vbox = new VBox();
        vbox.getChildren().add(table);
        
        
        
        Scene scene = new Scene(vbox, 500, 300);
        
        primaryStage.setTitle("TableView Demo");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    
    // Get all the Product objects into ObservableList method:
    // (This method is normally loading CSV file, connect to database, etc...)
    public ObservableList<Product> getProducts(){
        
        ObservableList<Product> products = FXCollections.observableArrayList();
        products.add(new Product("Laptop", 859, 20));
        products.add(new Product("Ball", 20, 40));
        products.add(new Product("Vase", 12.60, 10));
        products.add(new Product("Napkin", 1.29, 200));
        
        return products;
    }
 
    
    
    public static void main(String[] args) {
        launch(args);
    }
    
}

Open in new window


2. Product class, where the object properties expected to be shown in table are defined:
package tableviewdemo;
// The product is defined in a separate class

// Whenever we use tables in JavaFX behind the scenes it will look for a class like this,
// with getters and setters named in a standard way.
// (Custom names will break JavaFX)

public class Product {
    
    // each of these will be a column:
    private String name;
    private double price;
    private int quantity;
    
    // Default constructor:
    public Product(){
        this.name="";
        this.price = 0.0;
        this.quantity = 0;
    }
    
    // Overloaded constructor:
    public Product(String name, double price, int quantity){
        this.name = name;
        this.price = price;
        this.quantity = quantity;
    }
    
    // Getters and setters, with standard names;
    public String getName(){
       return this.name;
    }
    
    public double getPrice(){
       return this.price;
    }
    
    public int getQuantity(){
       return this.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;
    }
    
    
}

Open in new window


 I am using NetBeans 8.2 with Java SE 8, and making a JavaFX application.

What am I doing wrong?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AttilaB
AttilaB

ASKER

That was 'not so smart' from me... I am looking, but I am not seeing.  :)
Thanks.
:)