Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

JavaFX: TableView: Adding new rows to a table

This is continuation to my previous post. In this post, I am going to show, how to add new rows to a table.

I used below list to add data to a table.

         private static final ObservableList data = FXCollections.observableArrayList(
                           new Friend("Hari", "Krishna", "[email protected]"),
                           new Friend("Bhairava", "beddu", "[email protected]"),
                           new Friend("ahswani", "sharmaSekar", "[email protected]"),
                           new Friend("chandra", "babu", "[email protected]"));

At any point of time, you can add new row to a table by adding new object to the observable list ‘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.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class TableViewApp extends Application {
private static final ObservableListFriend> data = FXCollections.observableArrayList(
new Friend("Hari", "Krishna", "[email protected]"),
new Friend("Bhairava", "beddu", "[email protected]"),
new Friend("ahswani", "sharmaSekar", "[email protected]"),
new Friend("chandra", "babu", "[email protected]"));

@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);

final TextField addFirstName = new TextField();
addFirstName.setPromptText("First Name");
addFirstName.setMaxWidth(firstNameCol.getPrefWidth());

final TextField addLastName = new TextField();
addLastName.setMaxWidth(lastNameCol.getPrefWidth());
addLastName.setPromptText("Last Name");

final TextField addEmail = new TextField();
addEmail.setMaxWidth(emailCol.getPrefWidth());
addEmail.setPromptText("Email");

final Button addButton = new Button("Add");
addButton.setOnAction((ActionEvent e) -> {
data.add(new Friend(addFirstName.getText(), addLastName.getText(), addEmail.getText()));
addFirstName.clear();
addLastName.clear();
addEmail.clear();
});

HBox hBox = new HBox(10, addFirstName, addLastName, addEmail, addButton);

VBox vBox = new VBox(10, label, tableView, hBox);
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);
}
}





After adding new entry ‘Harini’ table changed like below.




Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

JavaFX: TableView: Adding new rows to a table

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×