Advertisement
Guest User

SplitPane, TableView, treeTableView behavior

a guest
Nov 4th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.55 KB | None | 0 0
  1. public class test extends Application {
  2.  
  3.      private TableView<Person> table = new TableView<Person>();
  4.         private final ObservableList<Person> data =
  5.                 FXCollections.observableArrayList(
  6.                 new Person("Jacob", "Smith", "jacob.smith@example.com"),
  7.                 new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
  8.                 new Person("Ethan", "Williams", "ethan.williams@example.com"),
  9.                 new Person("Emma", "Jones", "emma.jones@example.com"),
  10.                 new Person("Michael", "Brown", "michael.brown@example.com"));
  11.         final HBox hb = new HBox();
  12.      
  13.         public static void main(String[] args) {
  14.             launch(args);
  15.         }
  16.      
  17.         @Override
  18.         public void start(Stage stage) {
  19.             Scene scene = new Scene(new Group());
  20.             stage.setTitle("Table View Sample");
  21.             stage.setWidth(450);
  22.             stage.setHeight(550);
  23.             SplitPane splitPane = new SplitPane();
  24.             splitPane.setOrientation(Orientation.VERTICAL);
  25.            
  26.             //TREETABLECOLUMN
  27.             TreeTableView<Person> treeTableView = new TreeTableView<>();
  28.             TreeTableColumn temp = new TreeTableColumn("First Name");
  29.             temp.setMinWidth(100);
  30.             temp.setCellValueFactory(
  31.                     new PropertyValueFactory<Person, String>("firstName"));
  32.      
  33.             TreeTableColumn temp2 = new TreeTableColumn("Last Name");
  34.             temp2.setMinWidth(100);
  35.             temp2.setCellValueFactory(
  36.                     new PropertyValueFactory<Person, String>("lastName"));
  37.      
  38.             TreeTableColumn temp3 = new TreeTableColumn("Email");
  39.             temp3.setMinWidth(200);
  40.             temp3.setCellValueFactory(
  41.                     new PropertyValueFactory<Person, String>("email"));
  42.      
  43.             //treeTableView.setItems(data);
  44.             treeTableView.getColumns().addAll(temp, temp2, temp3);
  45.            
  46.             //TABLE
  47.             table.setEditable(true);
  48.             //This is important to notice the left and right selection not working
  49.             table.getSelectionModel().setCellSelectionEnabled(true);
  50.             TableColumn firstNameCol = new TableColumn("First Name");
  51.             firstNameCol.setMinWidth(100);
  52.             firstNameCol.setCellValueFactory(
  53.                     new PropertyValueFactory<Person, String>("firstName"));
  54.      
  55.             TableColumn lastNameCol = new TableColumn("Last Name");
  56.             lastNameCol.setMinWidth(100);
  57.             lastNameCol.setCellValueFactory(
  58.                     new PropertyValueFactory<Person, String>("lastName"));
  59.      
  60.             TableColumn emailCol = new TableColumn("Email");
  61.             emailCol.setMinWidth(200);
  62.             emailCol.setCellValueFactory(
  63.                     new PropertyValueFactory<Person, String>("email"));
  64.      
  65.             table.setItems(data);
  66.             table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
  67.      
  68.             final TextField addFirstName = new TextField();
  69.             addFirstName.setPromptText("First Name");
  70.             addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
  71.             final TextField addLastName = new TextField();
  72.             addLastName.setMaxWidth(lastNameCol.getPrefWidth());
  73.             addLastName.setPromptText("Last Name");
  74.             final TextField addEmail = new TextField();
  75.             addEmail.setMaxWidth(emailCol.getPrefWidth());
  76.             addEmail.setPromptText("Email");
  77.      
  78.             final Button addButton = new Button("Add");
  79.             addButton.setOnAction(new EventHandler<ActionEvent>() {
  80.                 @Override
  81.                 public void handle(ActionEvent e) {
  82.                     data.add(new Person(
  83.                             addFirstName.getText(),
  84.                             addLastName.getText(),
  85.                             addEmail.getText()));
  86.                     addFirstName.clear();
  87.                     addLastName.clear();
  88.                     addEmail.clear();
  89.                 }
  90.             });
  91.      
  92.             /**
  93.              * Try to add the TableView first and then the treeTableView,
  94.              * you will see that selection left and right are working.
  95.              *
  96.              */
  97.             //I add the TreeTableView FIRST
  98.             splitPane.getItems().add(treeTableView);
  99.             //Then I add the tableView
  100.             splitPane.getItems().add(table);
  101.            
  102.             //Second case working
  103.             //I add the tableView FIRST
  104.             //splitPane.getItems().add(table);
  105.             //Then I add the TreeTableView
  106.             //splitPane.getItems().add(treeTableView);
  107.            
  108.             ((Group) scene.getRoot()).getChildren().addAll(splitPane);
  109.      
  110.             stage.setScene(scene);
  111.             stage.show();
  112.         }
  113.      
  114.         public static class Person {
  115.      
  116.             private final SimpleStringProperty firstName;
  117.             private final SimpleStringProperty lastName;
  118.             private final SimpleStringProperty email;
  119.      
  120.             private Person(String fName, String lName, String email) {
  121.                 this.firstName = new SimpleStringProperty(fName);
  122.                 this.lastName = new SimpleStringProperty(lName);
  123.                 this.email = new SimpleStringProperty(email);
  124.             }
  125.      
  126.             public String getFirstName() {
  127.                 return firstName.get();
  128.             }
  129.      
  130.             public void setFirstName(String fName) {
  131.                 firstName.set(fName);
  132.             }
  133.      
  134.             public String getLastName() {
  135.                 return lastName.get();
  136.             }
  137.      
  138.             public void setLastName(String fName) {
  139.                 lastName.set(fName);
  140.             }
  141.      
  142.             public String getEmail() {
  143.                 return email.get();
  144.             }
  145.      
  146.             public void setEmail(String fName) {
  147.                 email.set(fName);
  148.             }
  149.         }
  150.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement