Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class test extends Application {
- private TableView<Person> table = new TableView<Person>();
- private final ObservableList<Person> data =
- FXCollections.observableArrayList(
- new Person("Jacob", "Smith", "jacob.smith@example.com"),
- new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
- new Person("Ethan", "Williams", "ethan.williams@example.com"),
- new Person("Emma", "Jones", "emma.jones@example.com"),
- new Person("Michael", "Brown", "michael.brown@example.com"));
- final HBox hb = new HBox();
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage stage) {
- Scene scene = new Scene(new Group());
- stage.setTitle("Table View Sample");
- stage.setWidth(450);
- stage.setHeight(550);
- SplitPane splitPane = new SplitPane();
- splitPane.setOrientation(Orientation.VERTICAL);
- //TREETABLECOLUMN
- TreeTableView<Person> treeTableView = new TreeTableView<>();
- TreeTableColumn temp = new TreeTableColumn("First Name");
- temp.setMinWidth(100);
- temp.setCellValueFactory(
- new PropertyValueFactory<Person, String>("firstName"));
- TreeTableColumn temp2 = new TreeTableColumn("Last Name");
- temp2.setMinWidth(100);
- temp2.setCellValueFactory(
- new PropertyValueFactory<Person, String>("lastName"));
- TreeTableColumn temp3 = new TreeTableColumn("Email");
- temp3.setMinWidth(200);
- temp3.setCellValueFactory(
- new PropertyValueFactory<Person, String>("email"));
- //treeTableView.setItems(data);
- treeTableView.getColumns().addAll(temp, temp2, temp3);
- //TABLE
- table.setEditable(true);
- //This is important to notice the left and right selection not working
- table.getSelectionModel().setCellSelectionEnabled(true);
- TableColumn firstNameCol = new TableColumn("First Name");
- firstNameCol.setMinWidth(100);
- firstNameCol.setCellValueFactory(
- new PropertyValueFactory<Person, String>("firstName"));
- TableColumn lastNameCol = new TableColumn("Last Name");
- lastNameCol.setMinWidth(100);
- lastNameCol.setCellValueFactory(
- new PropertyValueFactory<Person, String>("lastName"));
- TableColumn emailCol = new TableColumn("Email");
- emailCol.setMinWidth(200);
- emailCol.setCellValueFactory(
- new PropertyValueFactory<Person, String>("email"));
- table.setItems(data);
- table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
- 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(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent e) {
- data.add(new Person(
- addFirstName.getText(),
- addLastName.getText(),
- addEmail.getText()));
- addFirstName.clear();
- addLastName.clear();
- addEmail.clear();
- }
- });
- /**
- * Try to add the TableView first and then the treeTableView,
- * you will see that selection left and right are working.
- *
- */
- //I add the TreeTableView FIRST
- splitPane.getItems().add(treeTableView);
- //Then I add the tableView
- splitPane.getItems().add(table);
- //Second case working
- //I add the tableView FIRST
- //splitPane.getItems().add(table);
- //Then I add the TreeTableView
- //splitPane.getItems().add(treeTableView);
- ((Group) scene.getRoot()).getChildren().addAll(splitPane);
- stage.setScene(scene);
- stage.show();
- }
- public static class Person {
- private final SimpleStringProperty firstName;
- private final SimpleStringProperty lastName;
- private final SimpleStringProperty email;
- private Person(String fName, String lName, String email) {
- this.firstName = new SimpleStringProperty(fName);
- this.lastName = new SimpleStringProperty(lName);
- this.email = new SimpleStringProperty(email);
- }
- public String getFirstName() {
- return firstName.get();
- }
- public void setFirstName(String fName) {
- firstName.set(fName);
- }
- public String getLastName() {
- return lastName.get();
- }
- public void setLastName(String fName) {
- lastName.set(fName);
- }
- public String getEmail() {
- return email.get();
- }
- public void setEmail(String fName) {
- email.set(fName);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement