Advertisement
Guest User

JavaFX TableView with multiline column

a guest
Nov 10th, 2014
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.68 KB | None | 0 0
  1. package test;
  2.  
  3. import java.util.Comparator;
  4.  
  5. import javafx.application.Application;
  6. import javafx.beans.property.ReadOnlyObjectWrapper;
  7. import javafx.beans.property.SimpleStringProperty;
  8. import javafx.beans.value.ObservableValue;
  9. import javafx.collections.FXCollections;
  10. import javafx.collections.ObservableList;
  11. import javafx.geometry.Insets;
  12. import javafx.geometry.Pos;
  13. import javafx.scene.Group;
  14. import javafx.scene.Scene;
  15. import javafx.scene.control.Label;
  16. import javafx.scene.control.TableCell;
  17. import javafx.scene.control.TableColumn;
  18. import javafx.scene.control.TableColumn.CellDataFeatures;
  19. import javafx.scene.control.TableView;
  20. import javafx.scene.control.cell.PropertyValueFactory;
  21. import javafx.scene.layout.Pane;
  22. import javafx.scene.layout.Priority;
  23. import javafx.scene.layout.VBox;
  24. import javafx.scene.text.Font;
  25. import javafx.scene.text.Text;
  26. import javafx.scene.text.TextAlignment;
  27. import javafx.stage.Stage;
  28. import javafx.util.Callback;
  29.  
  30. public class TableTest extends Application {
  31.     private TableView<Person> table = new TableView<Person>();
  32.     private final ObservableList<Person> data =
  33.         FXCollections.observableArrayList(
  34.             new Person("Jacob", "Smith", "jacob.smith@example.com","Coffee"),
  35.             new Person("Isabella", "Johnson", "isabella.johnson@example.com","Fruit"),
  36.             new Person("Ethan", "Williams", "ethan.williams@example.com","Fruit"),
  37.             new Person("Emma", "Jones", "emma.jones@example.com","Coffee"),
  38.             new Person("Michael", "Brown", "michael.brown@example.com","Fruit")
  39.  
  40.         );
  41.  
  42.     public static void main(String[] args) {
  43.         launch(args);
  44.     }
  45.  
  46.     @Override
  47.     public void start(Stage stage) {
  48.         stage.setTitle("Table View Sample");
  49.  
  50.         final Label label = new Label("Address Book");
  51.         label.setFont(new Font("Arial", 20));
  52.  
  53.         final Label actionTaken = new Label();
  54.  
  55.         table.setEditable(true);
  56.  
  57.         TableColumn firstNameCol = new TableColumn("First Name");
  58.         firstNameCol.setMinWidth(100);
  59.         firstNameCol.setCellValueFactory(
  60.                 new PropertyValueFactory<Person, String>("firstName"));
  61.  
  62.         TableColumn lastNameCol = new TableColumn("Last Name");
  63.         lastNameCol.setMinWidth(100);
  64.         lastNameCol.setCellValueFactory(
  65.                 new PropertyValueFactory<Person, String>("lastName"));
  66.  
  67.         TableColumn emailCol = new TableColumn("Email");
  68.         emailCol.setMinWidth(200);
  69.         emailCol.setCellValueFactory(
  70.                 new PropertyValueFactory<Person, String>("email"));
  71.  
  72.         TableColumn<Person, Person> multiCol = new TableColumn<>("Multiline");
  73.         multiCol.setMinWidth(200);
  74.         multiCol.setCellValueFactory(new Callback<CellDataFeatures<Person, Person>, ObservableValue<Person>>() {
  75.           @Override public ObservableValue<Person> call(CellDataFeatures<Person, Person> features) {
  76.               return new ReadOnlyObjectWrapper(features.getValue());
  77.           }
  78.         });
  79.         multiCol.setComparator(new Comparator<Person>() {
  80.           @Override public int compare(Person p1, Person p2) {
  81.             return p1.getLikes().compareTo(p2.getLikes());
  82.           }
  83.         });
  84.         multiCol.setCellFactory(new Callback<TableColumn<Person, Person>, TableCell<Person, Person>>() {
  85.           @Override public TableCell<Person, Person> call(TableColumn<Person, Person> multiCol) {
  86.             return new TableCell<Person, Person>() {
  87.                private Group grp = null;
  88.  
  89.                @Override public void updateItem(final Person person, boolean empty) {
  90.                   super.updateItem(person, empty);
  91.  
  92.                   this.setAlignment(Pos.CENTER);
  93.  
  94.                   if (!isEmpty()) {
  95.                      Text text = new Text(person.getFirstName());
  96.                      text.setX(0);
  97.                      text.setY(0);
  98.                      text.setTextAlignment(TextAlignment.CENTER); // Center text?
  99.  
  100.                      Pane pane = new Pane();
  101.                      pane.setStyle("-fx-background-color: #66BB66;");
  102.                      pane.setLayoutX(0);
  103.                      pane.setLayoutY(0);
  104.                      pane.setPrefHeight(20);
  105.                      pane.setPrefWidth(this.prefWidth(-1)); // Column width?
  106.  
  107.                      // -----
  108.  
  109.                      Text text2 = new Text(person.getLastName());
  110.                      text2.setX(0);
  111.                      text2.setY(20);
  112.                      text2.setTextAlignment(TextAlignment.CENTER); // Center text?
  113.  
  114.                      Pane pane2 = new Pane();
  115.                      pane2.setStyle("-fx-background-color: #79A8D8;");
  116.                      pane2.setLayoutX(0);
  117.                      pane2.setLayoutY(20);
  118.                      pane2.setPrefHeight(20);
  119.                      pane2.setPrefWidth(this.prefWidth(-1)); // Column width?
  120.  
  121.                      // -----
  122.  
  123.                      Text text3 = new Text(person.getEmail());
  124.                      text3.setX(0);
  125.                      text3.setY(40);
  126.                      text3.setTextAlignment(TextAlignment.CENTER); // Center text?
  127.  
  128.                      Pane pane3 = new Pane();
  129.                      pane3.setStyle("-fx-background-color: #FF8888;");
  130.                      pane3.setLayoutX(0);
  131.                      pane3.setLayoutY(40);
  132.                      pane3.setPrefHeight(20);
  133.                      pane3.setPrefWidth(this.prefWidth(-1)); // Column width?
  134.  
  135.                      // -----
  136.  
  137.                      Group grp = new Group();
  138.  
  139.                      grp.getChildren().add(pane);
  140.                      grp.getChildren().add(text);
  141.  
  142.                      grp.getChildren().add(pane2);
  143.                      grp.getChildren().add(text2);
  144.  
  145.                      grp.getChildren().add(pane3);
  146.                      grp.getChildren().add(text3);
  147.  
  148.                      setGraphic(grp);
  149.  
  150.                      setStyle("-fx-padding: 0 0 0 0;");
  151.                   }
  152.                 }
  153.               };
  154.             }
  155.           });
  156.  
  157.         table.setItems(data);
  158.         table.getColumns().addAll(firstNameCol, lastNameCol, emailCol, multiCol);
  159.  
  160.         final VBox vbox = new VBox();
  161.         vbox.setSpacing(5);
  162.         vbox.setPadding(new Insets(10, 10, 10, 10));
  163.         vbox.getChildren().addAll(label, table, actionTaken);
  164.         VBox.setVgrow(table, Priority.ALWAYS);
  165.  
  166.         stage.setScene(new Scene(vbox));
  167.         stage.show();
  168.     }
  169.  
  170.     public static class Person {
  171.  
  172.         private final SimpleStringProperty firstName;
  173.         private final SimpleStringProperty lastName;
  174.         private final SimpleStringProperty email;
  175.         private final SimpleStringProperty likes;
  176.  
  177.         private Person(String fName, String lName, String email, String likes) {
  178.             this.firstName = new SimpleStringProperty(fName);
  179.             this.lastName = new SimpleStringProperty(lName);
  180.             this.email = new SimpleStringProperty(email);
  181.             this.likes = new SimpleStringProperty(likes);
  182.         }
  183.  
  184.         public String getFirstName() {
  185.             return firstName.get();
  186.         }
  187.  
  188.         public void setFirstName(String fName) {
  189.             firstName.set(fName);
  190.         }
  191.  
  192.         public String getLastName() {
  193.             return lastName.get();
  194.         }
  195.  
  196.         public void setLastName(String fName) {
  197.             lastName.set(fName);
  198.         }
  199.  
  200.         public String getEmail() {
  201.             return email.get();
  202.         }
  203.  
  204.         public void setEmail(String fName) {
  205.             email.set(fName);
  206.         }
  207.  
  208.         public String getLikes() {
  209.             return likes.get();
  210.         }
  211.  
  212.         public void setLikes(String likes) {
  213.             this.likes.set(likes);
  214.         }
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement