Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.application.Application;
- import javafx.beans.property.SimpleStringProperty;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.scene.Node;
- import javafx.scene.Parent;
- import javafx.scene.Scene;
- import javafx.scene.control.TableCell;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableRow;
- import javafx.scene.control.TableView;
- import javafx.scene.control.cell.PropertyValueFactory;
- import javafx.stage.Stage;
- public class FindingCell extends Application {
- private final TableView<Person> table = new TableView<>();
- private final ObservableList<Person> data
- = FXCollections.observableArrayList(
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage stage) {
- table.setEditable(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);
- Scene scene = new Scene(table);
- stage.setTitle("Table View Sample");
- stage.setWidth(450);
- stage.setHeight(550);
- stage.setScene(scene);
- stage.show();
- traverse(table, -1, -1);
- }
- public void traverse(Node node, int row, int column) {
- //System.out.println(node.getClass());
- int newRow = (node instanceof TableRow) ? ((TableRow) node).getIndex() : row;
- int newColumn = (node instanceof TableCell) ? ((TableCell)node).getIndex() : column;
- if (node.getClass().getName().contains("LabeledText")) {
- String text = ((com.sun.javafx.scene.control.skin.LabeledText) node).getText();
- if (!text.isEmpty()) {
- System.out.println("### " + text);
- System.out.println("# Row: " + row + " Column: " + column);
- System.out.println("# Height: " + node.getBoundsInParent().getHeight());
- }
- }
- if (node instanceof Parent) {
- Parent parent = (Parent) node;
- parent.getChildrenUnmodifiable().forEach(n -> traverse(n, newRow, newColumn));
- }
- }
- 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);
- }
- }
- }
Add Comment
Please, Sign In to add comment