Table View in Java Fx2.0 using Collection



In this example, person class is a pojo(Plain Old Java Object).In order to load a mutiple data in table view component i have used the collection in javaFx
// Comment
 ObservableList people = FXCollections.observableArrayList();
This is method by which we can get collection of data in to Array List.Then obtained list is loaded in the table view component of JavaFx2.0
person.java
// Comment
package javafxapplication2;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
 *
 * @author sabari
 */
public class person {

    private String firstName;
private ObservableList employees = FXCollections.observableArrayList();

    person() {
      
    }

    public ObservableList getEmployees() {
        return employees;
    }

    public void setEmployees(ObservableList employees) {
        this.employees = employees;
    }
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    private String lastName;

    public person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }



}

Main.java
// Comment
package javafxapplication2;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import javafx.stage.Stage;

public class Main extends Application {

    public void start(Stage stage) {
       
        stage.setTitle("Hello Table");

        Group root = new Group();
        Scene scene = new Scene(root);
        stage.setScene(scene);

       root.getChildren().add(getTableView());
        stage.show();
    }

    public TableView getTableView() {
        
     ObservableList people = FXCollections.observableArrayList();
        
 people.add(new person("Associate", "Javafx"));
 people.add(new person("Developer", "Hibernate"));
 people.add(new person("Best performer", "Java"));
 people.add(new person("Best Programmer", "Sabari Raja"));

        TableView tableView = new TableView();
   
        tableView.setItems(people);

        TableColumn firstNameCol = new TableColumn("First");
     
firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));
        TableColumn lastNameCol = new TableColumn("Last");
    
        lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));
      

        tableView.getColumns().addAll(firstNameCol, lastNameCol);

        return tableView;
    }

    public static void main(String[] args) {
      Application.launch(args);
    }


}




Output 

raja P Web Developer

2 comments:

  1. postingan yang anda buat bagus.........

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...