Suntechnique

https://stackoverflow.com/questions/44980073/get-cell-height

Jul 8th, 2017
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.beans.property.SimpleStringProperty;
  3. import javafx.collections.FXCollections;
  4. import javafx.collections.ObservableList;
  5. import javafx.scene.Node;
  6. import javafx.scene.Parent;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.TableCell;
  9. import javafx.scene.control.TableColumn;
  10. import javafx.scene.control.TableRow;
  11. import javafx.scene.control.TableView;
  12. import javafx.scene.control.cell.PropertyValueFactory;
  13. import javafx.stage.Stage;
  14.  
  15. public class FindingCell extends Application {
  16.  
  17.     private final TableView<Person> table = new TableView<>();
  18.     private final ObservableList<Person> data
  19.             = FXCollections.observableArrayList(
  20.                     new Person("Jacob", "Smith", "[email protected]"),
  21.                     new Person("Isabella", "Johnson", "[email protected]"),
  22.                     new Person("Ethan", "Williams", "[email protected]"),
  23.                     new Person("Emma", "Jones", "[email protected]"),
  24.                     new Person("Michael", "Brown", "[email protected]"));
  25.  
  26.     public static void main(String[] args) {
  27.         launch(args);
  28.     }
  29.  
  30.     @Override
  31.     public void start(Stage stage) {
  32.  
  33.         table.setEditable(true);
  34.  
  35.         TableColumn firstNameCol = new TableColumn("First Name");
  36.         firstNameCol.setMinWidth(100);
  37.         firstNameCol.setCellValueFactory(
  38.                 new PropertyValueFactory<Person, String>("firstName"));
  39.  
  40.         TableColumn lastNameCol = new TableColumn("Last Name");
  41.         lastNameCol.setMinWidth(100);
  42.         lastNameCol.setCellValueFactory(
  43.                 new PropertyValueFactory<Person, String>("lastName"));
  44.  
  45.         TableColumn emailCol = new TableColumn("Email");
  46.         emailCol.setMinWidth(200);
  47.         emailCol.setCellValueFactory(
  48.                 new PropertyValueFactory<Person, String>("email"));
  49.  
  50.         table.setItems(data);
  51.         table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
  52.  
  53.         Scene scene = new Scene(table);
  54.         stage.setTitle("Table View Sample");
  55.         stage.setWidth(450);
  56.         stage.setHeight(550);
  57.         stage.setScene(scene);
  58.         stage.show();
  59.  
  60.         traverse(table, -1, -1);
  61.     }
  62.  
  63.     public void traverse(Node node, int row, int column) {
  64.         //System.out.println(node.getClass());
  65.  
  66.         int newRow = (node instanceof TableRow) ? ((TableRow) node).getIndex() : row;
  67.         int newColumn = (node instanceof TableCell) ? ((TableCell)node).getIndex() : column;
  68.         if (node.getClass().getName().contains("LabeledText")) {
  69.             String text = ((com.sun.javafx.scene.control.skin.LabeledText) node).getText();
  70.             if (!text.isEmpty()) {
  71.                 System.out.println("### " + text);
  72.                 System.out.println("# Row: " + row + " Column: " + column);
  73.                 System.out.println("# Height: " + node.getBoundsInParent().getHeight());
  74.             }
  75.         }
  76.         if (node instanceof Parent) {
  77.             Parent parent = (Parent) node;
  78.             parent.getChildrenUnmodifiable().forEach(n -> traverse(n, newRow, newColumn));
  79.         }
  80.     }
  81.  
  82.     public static class Person {
  83.  
  84.         private final SimpleStringProperty firstName;
  85.         private final SimpleStringProperty lastName;
  86.         private final SimpleStringProperty email;
  87.  
  88.         private Person(String fName, String lName, String email) {
  89.             this.firstName = new SimpleStringProperty(fName);
  90.             this.lastName = new SimpleStringProperty(lName);
  91.             this.email = new SimpleStringProperty(email);
  92.         }
  93.  
  94.         public String getFirstName() {
  95.             return firstName.get();
  96.         }
  97.  
  98.         public void setFirstName(String fName) {
  99.             firstName.set(fName);
  100.         }
  101.  
  102.         public String getLastName() {
  103.             return lastName.get();
  104.         }
  105.  
  106.         public void setLastName(String fName) {
  107.             lastName.set(fName);
  108.         }
  109.  
  110.         public String getEmail() {
  111.             return email.get();
  112.         }
  113.  
  114.         public void setEmail(String fName) {
  115.             email.set(fName);
  116.         }
  117.     }
  118. }
Add Comment
Please, Sign In to add comment