JavaFX TableView tutorial

JavaFX TableView is used to represent the data in tabular form.

Means it is an extend version of list in which multiple columns supported.

Constructors of Table view:

  • TableView()
    Creates a default TableView control with no content.
  • TableView(ObservableList<S> items)
    Creates a TableView with the content provided in the items ObservableList.

Create TableView :

TableView table=new TableView();

Create column for TableView :

TableColumn cityCol = new TableColumn("Name");

Add the columns using getColumnd().addAll().

Example using table view :

package javafxtuts;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
 * @author javafxtuts
public class Javafxtuts extends Application {

  public void start(Stage primaryStage) {
      // setting title of application
      TableView table=new TableView();
      //create columns
      TableColumn NameCol = new TableColumn("Name");
      //create sub columns
      TableColumn fNameCol = new TableColumn("First Name");
      TableColumn lNameCol = new TableColumn("Last Name");
      //Add sub columns to the NameCol
      NameCol.getColumns().addAll(fNameCol, lNameCol);
      TableColumn addressCol = new TableColumn("Address");
     //add all column to table
    //create a layout Pane
    StackPane root = new StackPane();

      //to create a scene using sp
      Scene scene = new Scene(root, 250, 300);
     * @param args the command line arguments
    public static void main(String[] args) {

Output :

Explanation of table view example:

  • Creating TableView AS
    TableView table=new TableView();
  • Creating columns for table AS
    TableColumn NameCol = new TableColumn("Name");
    It create a column in of “Name”.
  • To make columns sub Columns of other just add these columns to main columns AS
    NameCol.getColumns().addAll(fNameCol, lNameCol);
  • Adding all columns to the TableView(table)
    table.getColumns().addAll( NameCol, addressCol);

Leave A Reply