Updated March 27, 2023
Introduction to TableView in JavaFX
TableView in JavaFX is used for showing the same columns data in a table-like structure. TableView in JavaFX can have multiple rows and columns to display the data. TableView data can also make editable by setEditabl() method. TableView has more data then we can have a scroll option to see the entire data. Real-Time Usage. All the JavaFX applications are standalone applications, so most of them are like installing products, Development Tools, Games, etc. When we install a product, there is a lot of information like version, software name, pre-requisites, etc. If this kind of data in a row like structure, it is a little bit ambiguous to figure out the things clearly. There we have used this TableView concept to align rows and columns in a table-like structure.
Advantages of Table View
- High readability because of the table-like structure.
- Scrolling with the table.
- Option to set editable cells.
How does TableView work in JavaFX?
- Accessing JavaFX features user-defined class must extend Application.
- Steps to create a TableView and displaying the table.
- Creating TableView can instantiate by using new
Syntax:
TableVIew tView=new TableView();
- Creating a TableColumn object to add columns to a table.
Syntax:
TableColumn columnName1=new TableColumn("Column Name");
TableColumn columnName2=new TableColumn("Column Name");
TableColumn columnName3=new TableColumn("Column Name");
- Adding columns to a table by using getColumns().addAll()
Syntax:
tView.getColumns().addAll(columnName1, columnName2, columnName3,…..);
- Create any component like a horizontal or vertical box to add the items.
Syntax:
HBox hBox=new HBox(); //Gives horizontal box
VBox vBox=new VBox();//Gives vertical box
- Adding table(tView) to the vBox or hBox by using getChildern().add()
Syntax:
vBox.getChildren().add(tView);
- Creating a scene means screen to display output.
Syntax:
Scene screen = new Scene(hBox or vBox, length, width);
- Adding Scene reference screen to the Stage object reference. Adding output screen to Stage. We will get this stage object reference from the start predefined JavaFX method.
Syntax:
stage.setScene(screen);
- At last display output screen by showing stage reference with the show ()
Syntax:
stage.show();
Code with Examples
Let us look at a few examples and code with a brief explanation.
Example #1 – Table with Columns Example
JavaFX Code: TableViewColumns.java
package com.tableview;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TableViewColumns extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage outStage) {
//creating table view object
TableView<String> table = new TableView<String>();
//Adding columns to the table
TableColumn javaColumn = new TableColumn("EDUCBA Java");
TableColumn cColumn = new TableColumn("EDUCBA C");
TableColumn cSharpColumn = new TableColumn("EDUCBA C#");
//adding columns to the table
table.getColumns().addAll(javaColumn, cColumn, cSharpColumn);
//creating VBox component
VBox vBox = new VBox();
//adding table to the vBox
vBox.getChildren().add(table);
//creating a scene for adding vBox
Scene scene = new Scene(vBox,250,200);
//Title for display on top of the application
outStage.setTitle("Columns Table");
//adding scene to the stage
outStage.setScene(scene);
//displayingoutput
outStage.show();
}
}
Output:
- The above code adding the 3 columns to the table without editable. (by default, the table is non-editable)
Example #2 -Adding data to a table column
JavaFX Code: AddingData.java
package com.tableview;
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 AddingData extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage outStage) {
// creating table view object
TableView<Courses> table = new TableView<Courses>();
//obserbale list for taking multiple values at a time
ObservableList<Courses> courseData = FXCollections.observableArrayList(
new Courses("Inheritance"),
new Courses("Abstraction"),
new Courses("Polymorphism"),
new Courses("Generics"),
new Courses("OOPS"),
new Courses("Functions"));
// Adding columns to the table
TableColumn javaColumn = new TableColumn("EDUCBA Java");
//adding column value
javaColumn.setCellValueFactory(new PropertyValueFactory<Courses, String>("javaCorse"));
//setting minimum width
javaColumn.setMinWidth(100);
// adding columns to the table
table.getColumns().addAll(javaColumn);
// table.setItems(courseData);
table.setItems(courseData);
// creating VBox component
VBox vBox = new VBox();
// adding table to the vBox
vBox.getChildren().add(table);
// creating a scene for adding vBox
Scene scene = new Scene(vBox, 400, 400);
// Title for display on top of the application
outStage.setTitle("Courses List");
// adding scene to the stage
outStage.setScene(scene);
// displayingoutput
outStage.show();
}
public static class Courses {
private String javaCorse;
public String getJavaCorse() {
return javaCorse;
}
public void setJavaCorse(String javaCorse) {
this.javaCorse = javaCorse;
}
public Courses(String javaCorse) {
super();
this.javaCorse = javaCorse;
}
}
}
Output:
- The above code Courses class has declared with variable and constructor to take multiple values to the table.
- The observable list allows a list of values at a time.
- setCellValueFactory() adds rows to the table.
Example #3 – Adding Column inside a column
JavaFX Code: NestedColumn.java
package com.tableview;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class NestedColumns extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage outStage) {
//creating table view object
TableView<String> table = new TableView<String>();
//Adding columns to the table
TableColumn javaColumn = new TableColumn("EDUCBA Java");
TableColumn cColumn = new TableColumn("EDUCBA C");
TableColumn pythonColumn = new TableColumn("EDUCBA Python");
TableColumn machineLearing = new TableColumn("MachineLearing");
TableColumn augmentedReality = new TableColumn("Augmented Reality");
pythonColumn.getColumns().addAll(machineLearing,augmentedReality);
//adding columns to the table
table.getColumns().addAll(javaColumn, cColumn, pythonColumn);
//creating VBox component
VBox vBox = new VBox();
vBox.setSpacing(5);
//adding table to the vBox
vBox.getChildren().add(table);
//creating a scene for adding vBox
Scene scene = new Scene(vBox,350,250);
//Title for display on top of the application
outStage.setTitle("Nested Columns Table");
//adding scene to the stage
outStage.setScene(scene);
//displayingoutput
outStage.show();
}
}
Output:
Explanation:
- As you can see in the code, you can add as many nested columns as we want.
Recommended Articles
This is a guide to TableView in JavaFX. Here we discuss the Introduction, syntax, and examples of TableView. You may also have a look at the following articles to learn more –