TableView_Simple1
Scope : TableView class represents a TableView control.
  • TableView Class
    •  TableColumn: a column in a TableRow
    • TableRow: inherits IndexedCell Class
    • TableCell
    • TablePosition
    • TableView.TableViewFocusModel
    • TableView.TableViewSelectionModel
  • The columns in TableView has sorting properties 
  • In this example all column has  javafx.beans.property.SimpleStringProperty;
Person class:: getter/setter Properties:

Code : TableView_Simple1.txt


package javafxtemplate1;

import java.sql.SQLException;
import javafx.application.Application;
//import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;



public class JavaFXTemplate1 extends Application {
private final TableView<Person> table = new TableView<>();
private final ObservableList<Person> dataRow =
FXCollections.observableArrayList(
new Person("John", "Smith", "Boston"),
new Person("Jill", "Nelson", "Boston"),
new Person("Harison", "Festo", "Ohio"),
new Person("Akash", "Menon", "New York"),
new Person("Sanjeeb", "Goel", "Delhi"),
new Person("Peter", "Pan", "Chicago")
);
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(new Group());
primaryStage.setWidth(300);
primaryStage.setHeight(250);

table.setEditable(true);
// first column First name
TableColumn<Person, String> firstNameCol =
new TableColumn<>("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<>("firstName"));

firstNameCol.setCellFactory(TextFieldTableCell.<Person>forTableColumn());
firstNameCol.setOnEditCommit(
(CellEditEvent<Person, String> t) -> {
((Person) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setFirstName(t.getNewValue());
});

// second coloumn Last name
TableColumn<Person, String> lastNameCol =
new TableColumn<>("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
new PropertyValueFactory<>("lastName"));
lastNameCol.setCellFactory(TextFieldTableCell.<Person>forTableColumn());
lastNameCol.setOnEditCommit(
(CellEditEvent<Person, String> t) -> {
((Person) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setLastName(t.getNewValue());
});
//

TableColumn<Person, String> locNameCol =
new TableColumn<>("Location");
locNameCol.setMinWidth(100);
locNameCol.setCellValueFactory(
new PropertyValueFactory<>("location"));
locNameCol.setCellFactory(TextFieldTableCell.<Person>forTableColumn());
locNameCol.setOnEditCommit(
(CellEditEvent<Person, String> t) -> {
((Person) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setLocation(t.getNewValue());
});


//
table.setItems(dataRow);
table.getColumns().addAll(firstNameCol, lastNameCol,locNameCol );

final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll( table);

((Group) scene.getRoot()).getChildren().addAll(vbox);

primaryStage.setScene(scene);
primaryStage.show();

}



public static void main(String[] args) throws SQLException {
launch(args);
}


}

//class Person

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

/**
*
* @author Manas9
*/
public class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty location;
public Person(String fName, String lName, String city) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.location= new SimpleStringProperty(city);
}

public String getFirstName() { return firstName.get(); }
public void setFirstName(String fName) { firstName.set(fName);}
//
public String getLastName() {return lastName.get(); }
public void setLastName(String lName) { lastName.set(lName);}
//
public String getLocation() {return location.get(); }
public void setLocation(String city) { location.set(city); }

}

 

Runtime View:

Sorting Firstname

: