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<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"));
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.
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 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); 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.
FINALLY A WORKING TABLEVIEW CODE FOR INTELLIj YHANK YOUUU
ReplyDeleteMan, God bless you
ReplyDelete