JavaFX TableView with dynamically filled data from a List

Geno Tech
3 min readDec 29, 2020

Are you a JavaFX developer or a beginner in Java? This is a one problem Java developers facing most commonly. When you load data into a TableView, there are so many ways to do it and the lists contain custom objects(classes) can be difficult to bind. I will show you every step you want to get into success. You can read this carefully and your solution would apply easily in your own way. This is the easiest way.

  • Create a Empoloyee.java Class as Follows.
public class Employee {
private String name;
private int salary;
Employee(String name, int salary){ this.name = name;
this.salary = salary;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
  • Create an Employee data list and add into an Employees list

Here you create an Employee List and add some employee objects to it. We will display those data using a TableView. This code block shows your main() method. GUI will initialize and run inside another JavaFX Application class.

List<Employee> employeeList = new ArrayList<Employee>(); Employee emp1 = new Employee("Liam",20000);
employeeList.add(emp1);
Employee emp2 = new Employee("Noah",25000);
employeeList.add(emp2);
Employee emp3 = new Employee("William",30000);
employeeList.add(emp3);
Employee emp4 = new Employee("James",27000);
employeeList.add(emp4);
Employee emp5 = new Employee("Benjamin",23000);
employeeList.add(emp5);
Scanner menu = new Scanner(System.in); System.out.println("Enter 1 to display the table : "); int guess = menu.nextInt(); switch (guess) {
case 1:
showGUI(employeeList);
break;
default:
System.out.println("Input is invalid");
}
  • Create the JavaFX Application class

This step shows how to load the Employee list into a table. First, we want to Create a TableView and make two columns and add into the tableView.

// New Table View
TableView tbv = new TableView();
// Create two columns
TableColumn<String, Employee> cl1 = new TableColumn<>("Employee Name");
cl1.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<Integer, Employee> cl2 = new TableColumn<>("Employee Salary");
cl2.setCellValueFactory(new PropertyValueFactory<>("salary"));
// Add two columns into TableView
tbv.getColumns().add(cl1);
tbv.getColumns().add(cl2);

Thereafter we load our data one by one using a for a loop.

// Load objects into table
for (Employee emp : employeeList){
tbv.getItems().add(emp);
}

Then we show our table within a VBox.

// Display the table
VBox vbox = new VBox();
vbox.getChildren().addAll(tbv);
vbox.setSpacing(10);
vbox.setAlignment(Pos.CENTER);
Scene scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.show();

The final output as follows:

Finally, the complete JavaFX Application class can show as following.

public class Table extends Application {    private static List<Employee> employeeList;    public static void main(String[] args) {
launch(args);
}
public static void setManager(List<Employee> empList) {
employeeList = empList;
}
@Override
public void start(Stage primaryStage) {
TableView tbv = new TableView(); TableColumn<String, Employee> cl1 = new TableColumn<>("Employee Name");
cl1.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<Integer, Employee> cl2 = new TableColumn<>("Employee Salary");
cl2.setCellValueFactory(new PropertyValueFactory<>("salary"));
tbv.getColumns().add(cl1);
tbv.getColumns().add(cl2);
for (Employee emp : employeeList){
tbv.getItems().add(emp);
}
VBox vbox = new VBox();
vbox.getChildren().addAll(tbv);
vbox.setSpacing(10);
vbox.setAlignment(Pos.CENTER);
Scene scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.show();
}
}

Conclusion

This story gives you the simple way to load a list data(With custom objects) into a JavaFX TableView. Please feel free to ask any question you will face in the response section below.

Happy Coding !!!!

Found this post useful? Kindly tap the 👏 button below! :)

--

--

Geno Tech

Software Development | Data Science | AI — We write rich & meaningful content on development, technology, digital transformation & life lessons.