Sunday, 17 June 2018

JavaFX: Add data to a table

By adding data to 'setItems' method of TableView class, you can add data to a table. Below step-by-step procedure explains how to add data to a table.

Step 1: Create model class Friend. By using Friend class, I am going to feed data to a table.

Friend.java
package com.sample.model;

public class Friend {
	private final String firstName;
	private final String lastName;
	private final String email;

	public Friend(String fName, String lName, String email) {
		this.firstName = fName;
		this.lastName = lName;
		this.email = email;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public String getEmail() {
		return email;
	}

}

Step 2: Create table widget.
TableView tableView = new TableView();

Step 3: Add columns to a table.

TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(300);

TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(300);

TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(300);

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

Step 4: Prepare data to feed the table view widget.

         private static final ObservableList<Friend> data = FXCollections.observableArrayList(
                           new Friend("Hari", "Krishna", "krishna@krishna.com"),
                           new Friend("Bhairava", "beddu", "bhairava@bhairava.com"),
                           new Friend("ahswani", "sharmaSekar", "ashwani@ashwani.com"),
                           new Friend("chandra", "babu", "chandra@babu.com"));

Step 5: Set the cell value factory to table columns.

firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
emailCol.setCellValueFactory(new PropertyValueFactory<>("email"));

The setCellValueFactory method specifies a cell factory for each column. The cell
factories are implemented by using the PropertyValueFactory class, which uses the
firstName, lastName, and email properties of the table columns as references to the
corresponding methods of the Friend class.

Step 6: Add the data to table view.
tableView.setItems(data);

Find the below working application.


Friend.java

package com.sample.model;

public class Friend {
	private final String firstName;
	private final String lastName;
	private final String email;

	public Friend(String fName, String lName, String email) {
		this.firstName = fName;
		this.lastName = lName;
		this.email = email;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public String getEmail() {
		return email;
	}

}

TableViewApp.java

package com.sample.demos;

import com.sample.model.Friend;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class TableViewApp extends Application {
	private static final ObservableList<Friend> data = FXCollections.observableArrayList(
			new Friend("Hari", "Krishna", "krishna@krishna.com"),
			new Friend("Bhairava", "beddu", "bhairava@bhairava.com"),
			new Friend("ahswani", "sharmaSekar", "ashwani@ashwani.com"),
			new Friend("chandra", "babu", "chandra@babu.com"));

	@Override
	public void start(Stage primaryStage) {
		Label label = new Label("My Friends List");
		label.setFont(new Font("Arial", 30));

		TableView tableView = new TableView();

		TableColumn firstNameCol = new TableColumn("First Name");
		firstNameCol.setMinWidth(300);

		TableColumn lastNameCol = new TableColumn("Last Name");
		lastNameCol.setMinWidth(300);

		TableColumn emailCol = new TableColumn("Email");
		emailCol.setMinWidth(300);

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

		firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
		lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
		emailCol.setCellValueFactory(new PropertyValueFactory<>("email"));

		tableView.setItems(data);

		VBox vBox = new VBox(10, label, tableView);
		vBox.setSpacing(5);
		vBox.setPadding(new Insets(10, 10, 10, 10));

		primaryStage.setScene(new Scene(vBox));
		primaryStage.setTitle("Table View Example");
		primaryStage.setWidth(900);
		primaryStage.setHeight(500);
		primaryStage.show();
	}

}

TestFX.java

package com.sample.demos;

import javafx.application.Application;

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


Previous                                                 Next                                                 Home

No comments:

Post a Comment