Advertisement
Guest User

TableView height layout

a guest
Nov 25th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.80 KB | None | 0 0
  1.  
  2. import javafx.application.Application;
  3. import javafx.beans.property.SimpleStringProperty;
  4. import javafx.collections.FXCollections;
  5. import javafx.collections.ObservableList;
  6. import javafx.event.EventHandler;
  7. import javafx.scene.Group;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.TableColumn;
  10. import javafx.scene.control.TableColumn.CellEditEvent;
  11. import javafx.scene.control.TableView;
  12. import javafx.scene.control.cell.PropertyValueFactory;
  13. import javafx.scene.control.cell.TextFieldTableCell;
  14. import javafx.scene.layout.HBox;
  15. import javafx.stage.Stage;
  16.  
  17. public class test extends Application {
  18.  
  19.     private TableView<Person> table = new TableView<Person>();
  20.     private final ObservableList<Person> data = FXCollections
  21.             .observableArrayList(new Person("Jacob", "Smith",
  22.                     "[email protected]"), new Person("Isabella",
  23.                     "Johnson", "[email protected]"), new Person(
  24.                     "Ethan", "Williams", "[email protected]"),
  25.                     new Person("Emma", "Jones", "[email protected]"),
  26.                     new Person("Jacob", "Smith", "[email protected]"),
  27.                     new Person("Isabella", "Johnson",
  28.                             "[email protected]"), new Person(
  29.                             "Ethan", "Williams", "[email protected]"),
  30.                     new Person("Emma", "Jones", "[email protected]"),
  31.                     new Person("Jacob", "Smith", "[email protected]"),
  32.                     new Person("Isabella", "Johnson",
  33.                             "[email protected]"), new Person(
  34.                             "Ethan", "Williams", "[email protected]"),
  35.                     new Person("Emma", "Jones", "[email protected]"),
  36.                     new Person("Jacob", "Smith", "[email protected]"),
  37.                     new Person("Isabella", "Johnson",
  38.                             "[email protected]"), new Person(
  39.                             "Ethan", "Williams", "[email protected]"),
  40.                     new Person("Emma", "Jones", "[email protected]"),
  41.                     new Person("Jacob", "Smith", "[email protected]"),
  42.                     new Person("Isabella", "Johnson",
  43.                             "[email protected]"), new Person(
  44.                             "Ethan", "Williams", "[email protected]"),
  45.                     new Person("Emma", "Jones", "[email protected]"),
  46.                     new Person("Jacob", "Smith", "[email protected]"),
  47.                     new Person("Isabella", "Johnson",
  48.                             "[email protected]"), new Person(
  49.                             "Ethan", "Williams", "[email protected]"),
  50.                     new Person("Emma", "Jones", "[email protected]"),
  51.                     new Person("Jacob", "Smith", "[email protected]"),
  52.                     new Person("Isabella", "Johnson",
  53.                             "[email protected]"), new Person(
  54.                             "Ethan", "Williams", "[email protected]"),
  55.                     new Person("Emma", "Jones", "[email protected]"),
  56.                     new Person("Jacob", "Smith", "[email protected]"),
  57.                     new Person("Isabella", "Johnson",
  58.                             "[email protected]"), new Person(
  59.                             "Ethan", "Williams", "[email protected]"),
  60.                     new Person("Emma", "Jones", "[email protected]"),
  61.                     new Person("Jacob", "Smith", "[email protected]"),
  62.                     new Person("Isabella", "Johnson",
  63.                             "[email protected]"), new Person(
  64.                             "Ethan", "Williams", "[email protected]"),
  65.                     new Person("Emma", "Jones", "[email protected]"),
  66.                     new Person("Michael", "Brown", "[email protected]"));
  67.     final HBox hb = new HBox();
  68.  
  69.     public static void main(String[] args) {
  70.         launch(args);
  71.     }
  72.  
  73.     @Override
  74.     public void start(Stage stage) {
  75.         Scene scene = new Scene(new Group());
  76.         stage.setTitle("Table View Sample");
  77.         stage.setWidth(450);
  78.         stage.setHeight(550);
  79.  
  80.         table.setEditable(true);
  81.         table.prefHeightProperty().bind(stage.heightProperty());
  82.         TableColumn firstNameCol = new TableColumn("First Name");
  83.         firstNameCol.setMinWidth(100);
  84.         firstNameCol
  85.                 .setCellValueFactory(new PropertyValueFactory<Person, String>(
  86.                         "firstName"));
  87.         firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
  88.         firstNameCol
  89.                 .setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
  90.                     @Override
  91.                     public void handle(CellEditEvent<Person, String> t) {
  92.                         ((Person) t.getTableView().getItems()
  93.                                 .get(t.getTablePosition().getRow()))
  94.                                 .setFirstName(t.getNewValue());
  95.                     }
  96.                 });
  97.  
  98.         TableColumn lastNameCol = new TableColumn("Last Name");
  99.         lastNameCol.setMinWidth(100);
  100.         lastNameCol
  101.                 .setCellValueFactory(new PropertyValueFactory<Person, String>(
  102.                         "lastName"));
  103.         lastNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
  104.         lastNameCol
  105.                 .setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
  106.                     @Override
  107.                     public void handle(CellEditEvent<Person, String> t) {
  108.                         ((Person) t.getTableView().getItems()
  109.                                 .get(t.getTablePosition().getRow()))
  110.                                 .setLastName(t.getNewValue());
  111.                     }
  112.                 });
  113.  
  114.         TableColumn emailCol = new TableColumn("Email");
  115.         emailCol.setMinWidth(200);
  116.         emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>(
  117.                 "email"));
  118.         emailCol.setCellFactory(TextFieldTableCell.forTableColumn());
  119.         emailCol.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
  120.             @Override
  121.             public void handle(CellEditEvent<Person, String> t) {
  122.                 ((Person) t.getTableView().getItems()
  123.                         .get(t.getTablePosition().getRow())).setEmail(t
  124.                         .getNewValue());
  125.             }
  126.         });
  127.  
  128.         table.setItems(data);
  129.         table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
  130.  
  131.         ((Group) scene.getRoot()).getChildren().addAll(table);
  132.  
  133.         stage.setScene(scene);
  134.         stage.show();
  135.     }
  136.  
  137.     public static class Person {
  138.  
  139.         private final SimpleStringProperty firstName;
  140.         private final SimpleStringProperty lastName;
  141.         private final SimpleStringProperty email;
  142.  
  143.         private Person(String fName, String lName, String email) {
  144.             this.firstName = new SimpleStringProperty(fName);
  145.             this.lastName = new SimpleStringProperty(lName);
  146.             this.email = new SimpleStringProperty(email);
  147.         }
  148.  
  149.         public String getFirstName() {
  150.             return firstName.get();
  151.         }
  152.  
  153.         public void setFirstName(String fName) {
  154.             firstName.set(fName);
  155.         }
  156.  
  157.         public String getLastName() {
  158.             return lastName.get();
  159.         }
  160.  
  161.         public void setLastName(String fName) {
  162.             lastName.set(fName);
  163.         }
  164.  
  165.         public String getEmail() {
  166.             return email.get();
  167.         }
  168.  
  169.         public void setEmail(String fName) {
  170.             email.set(fName);
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement