Search in sources :

Example 1 with CellEditEvent

use of javafx.scene.control.TreeTableColumn.CellEditEvent in project JFoenix by jfoenixadmin.

the class TreeTableDemo method start.

@Override
public void start(Stage primaryStage) throws Exception {
    try {
        JFXTreeTableColumn<User, String> deptColumn = new JFXTreeTableColumn<>("Department");
        deptColumn.setPrefWidth(150);
        deptColumn.setCellValueFactory((TreeTableColumn.CellDataFeatures<User, String> param) -> {
            if (deptColumn.validateValue(param))
                return param.getValue().getValue().department;
            else
                return deptColumn.getComputedValue(param);
        });
        JFXTreeTableColumn<User, String> empColumn = new JFXTreeTableColumn<>("Employee");
        empColumn.setPrefWidth(150);
        empColumn.setCellValueFactory((TreeTableColumn.CellDataFeatures<User, String> param) -> {
            if (empColumn.validateValue(param))
                return param.getValue().getValue().userName;
            else
                return empColumn.getComputedValue(param);
        });
        JFXTreeTableColumn<User, String> ageColumn = new JFXTreeTableColumn<>("Age");
        ageColumn.setPrefWidth(150);
        ageColumn.setCellValueFactory((TreeTableColumn.CellDataFeatures<User, String> param) -> {
            if (ageColumn.validateValue(param))
                return param.getValue().getValue().age;
            else
                return ageColumn.getComputedValue(param);
        });
        ageColumn.setCellFactory((TreeTableColumn<User, String> param) -> new GenericEditableTreeTableCell<User, String>(new TextFieldEditorBuilder()));
        ageColumn.setOnEditCommit((CellEditEvent<User, String> t) -> {
            ((User) t.getTreeTableView().getTreeItem(t.getTreeTablePosition().getRow()).getValue()).age.set(t.getNewValue());
        });
        empColumn.setCellFactory((TreeTableColumn<User, String> param) -> new GenericEditableTreeTableCell<User, String>(new TextFieldEditorBuilder()));
        empColumn.setOnEditCommit((CellEditEvent<User, String> t) -> {
            ((User) t.getTreeTableView().getTreeItem(t.getTreeTablePosition().getRow()).getValue()).userName.set(t.getNewValue());
        });
        deptColumn.setCellFactory((TreeTableColumn<User, String> param) -> new GenericEditableTreeTableCell<User, String>(new TextFieldEditorBuilder()));
        deptColumn.setOnEditCommit((CellEditEvent<User, String> t) -> {
            ((User) t.getTreeTableView().getTreeItem(t.getTreeTablePosition().getRow()).getValue()).department.set(t.getNewValue());
        });
        // data
        ObservableList<User> users = FXCollections.observableArrayList();
        users.add(new User("Computer Department", "23", "CD 1"));
        users.add(new User("Sales Department", "22", "Employee 1"));
        users.add(new User("Sales Department", "22", "Employee 2"));
        users.add(new User("Sales Department", "25", "Employee 4"));
        users.add(new User("Sales Department", "25", "Employee 5"));
        users.add(new User("IT Department", "42", "ID 2"));
        users.add(new User("HR Department", "22", "HR 1"));
        users.add(new User("HR Department", "22", "HR 2"));
        for (int i = 0; i < 40000; i++) {
            users.add(new User("HR Department", i % 10 + "", "HR 2" + i));
        }
        for (int i = 0; i < 40000; i++) {
            users.add(new User("Computer Department", i % 20 + "", "CD 2" + i));
        }
        for (int i = 0; i < 40000; i++) {
            users.add(new User("IT Department", i % 5 + "", "HR 2" + i));
        }
        // build tree
        final TreeItem<User> root = new RecursiveTreeItem<User>(users, RecursiveTreeObject::getChildren);
        JFXTreeTableView<User> treeView = new JFXTreeTableView<User>(root);
        treeView.setShowRoot(false);
        treeView.setEditable(true);
        treeView.getColumns().setAll(deptColumn, ageColumn, empColumn);
        FlowPane main = new FlowPane();
        main.setPadding(new Insets(10));
        main.getChildren().add(treeView);
        JFXButton groupButton = new JFXButton("Group");
        groupButton.setOnAction((action) -> {
            new Thread(() -> treeView.group(empColumn)).start();
        });
        main.getChildren().add(groupButton);
        JFXButton unGroupButton = new JFXButton("unGroup");
        unGroupButton.setOnAction((action) -> treeView.unGroup(empColumn));
        main.getChildren().add(unGroupButton);
        JFXTextField filterField = new JFXTextField();
        main.getChildren().add(filterField);
        Label size = new Label();
        filterField.textProperty().addListener((o, oldVal, newVal) -> {
            treeView.setPredicate(user -> user.getValue().age.get().contains(newVal) || user.getValue().department.get().contains(newVal) || user.getValue().userName.get().contains(newVal));
        });
        size.textProperty().bind(Bindings.createStringBinding(() -> treeView.getCurrentItemsCount() + "", treeView.currentItemsCountProperty()));
        main.getChildren().add(size);
        Scene scene = new Scene(main, 475, 500);
        scene.getStylesheets().add(TreeTableDemo.class.getResource("/resources/css/jfoenix-components.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    //						ScenicView.show(scene);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Insets(javafx.geometry.Insets) Label(javafx.scene.control.Label) Scene(javafx.scene.Scene) CellEditEvent(javafx.scene.control.TreeTableColumn.CellEditEvent) TextFieldEditorBuilder(com.jfoenix.controls.cells.editors.TextFieldEditorBuilder) TreeTableColumn(javafx.scene.control.TreeTableColumn) FlowPane(javafx.scene.layout.FlowPane) RecursiveTreeObject(com.jfoenix.controls.datamodels.treetable.RecursiveTreeObject)

Example 2 with CellEditEvent

use of javafx.scene.control.TreeTableColumn.CellEditEvent in project JFoenix by jfoenixadmin.

the class TreeTableViewController method init.

@PostConstruct
public void init() throws FlowException, VetoException {
    String[] names = { "Morley", "Scott", "Kruger", "Lain", "Kennedy", "Gawron", "Han", "Hall", "Aydogdu", "Grace", "Spiers", "Perera", "Smith", "Connoly", "Sokolowski", "Chaow", "James", "June" };
    Random random = new Random();
    firstNameColumn.setCellValueFactory((TreeTableColumn.CellDataFeatures<Person, String> param) -> {
        if (firstNameColumn.validateValue(param))
            return param.getValue().getValue().firstName;
        else
            return firstNameColumn.getComputedValue(param);
    });
    lastNameColumn.setCellValueFactory((TreeTableColumn.CellDataFeatures<Person, String> param) -> {
        if (lastNameColumn.validateValue(param))
            return param.getValue().getValue().lastName;
        else
            return lastNameColumn.getComputedValue(param);
    });
    ageColumn.setCellValueFactory((TreeTableColumn.CellDataFeatures<Person, Integer> param) -> {
        if (ageColumn.validateValue(param))
            return param.getValue().getValue().age.asObject();
        else
            return ageColumn.getComputedValue(param);
    });
    ObservableList<Person> people = FXCollections.observableArrayList();
    for (int i = 0; i < 100; i++) people.add(new Person(names[random.nextInt(names.length)], names[random.nextInt(names.length)], random.nextInt(100)));
    treeTableView.setRoot(new RecursiveTreeItem<Person>(people, RecursiveTreeObject::getChildren));
    treeTableView.setShowRoot(false);
    treeTableViewCount.textProperty().bind(Bindings.createStringBinding(() -> "( " + treeTableView.getCurrentItemsCount() + " )", treeTableView.currentItemsCountProperty()));
    treeTableViewAdd.disableProperty().bind(Bindings.notEqual(-1, treeTableView.getSelectionModel().selectedIndexProperty()));
    treeTableViewRemove.disableProperty().bind(Bindings.equal(-1, treeTableView.getSelectionModel().selectedIndexProperty()));
    treeTableViewAdd.setOnMouseClicked((e) -> {
        people.add(new Person(names[random.nextInt(names.length)], names[random.nextInt(names.length)], random.nextInt(100)));
        treeTableView.currentItemsCountProperty().set(treeTableView.currentItemsCountProperty().get() + 1);
    });
    treeTableViewRemove.setOnMouseClicked((e) -> {
        people.remove(treeTableView.getSelectionModel().selectedItemProperty().get().getValue());
        treeTableView.currentItemsCountProperty().set(treeTableView.currentItemsCountProperty().get() - 1);
    });
    searchField.textProperty().addListener((o, oldVal, newVal) -> {
        treeTableView.setPredicate(person -> person.getValue().firstName.get().contains(newVal) || person.getValue().lastName.get().contains(newVal) || (person.getValue().age.get() + "").contains(newVal));
    });
    firstNameEditableColumn.setCellValueFactory((TreeTableColumn.CellDataFeatures<Person, String> param) -> {
        if (firstNameEditableColumn.validateValue(param))
            return param.getValue().getValue().firstName;
        else
            return firstNameEditableColumn.getComputedValue(param);
    });
    lastNameEditableColumn.setCellValueFactory((TreeTableColumn.CellDataFeatures<Person, String> param) -> {
        if (lastNameEditableColumn.validateValue(param))
            return param.getValue().getValue().lastName;
        else
            return lastNameEditableColumn.getComputedValue(param);
    });
    ageEditableColumn.setCellValueFactory((TreeTableColumn.CellDataFeatures<Person, Integer> param) -> {
        if (ageEditableColumn.validateValue(param))
            return param.getValue().getValue().age.asObject();
        else
            return ageEditableColumn.getComputedValue(param);
    });
    // add editors
    firstNameEditableColumn.setCellFactory((TreeTableColumn<Person, String> param) -> new GenericEditableTreeTableCell<Person, String>(new TextFieldEditorBuilder()));
    firstNameEditableColumn.setOnEditCommit((CellEditEvent<Person, String> t) -> {
        ((Person) t.getTreeTableView().getTreeItem(t.getTreeTablePosition().getRow()).getValue()).firstName.set(t.getNewValue());
    });
    lastNameEditableColumn.setCellFactory((TreeTableColumn<Person, String> param) -> new GenericEditableTreeTableCell<Person, String>(new TextFieldEditorBuilder()));
    lastNameEditableColumn.setOnEditCommit((CellEditEvent<Person, String> t) -> {
        ((Person) t.getTreeTableView().getTreeItem(t.getTreeTablePosition().getRow()).getValue()).lastName.set(t.getNewValue());
    });
    ageEditableColumn.setCellFactory((TreeTableColumn<Person, Integer> param) -> new GenericEditableTreeTableCell<Person, Integer>(new IntegerTextFieldEditorBuilder()));
    ageEditableColumn.setOnEditCommit((CellEditEvent<Person, Integer> t) -> {
        ((Person) t.getTreeTableView().getTreeItem(t.getTreeTablePosition().getRow()).getValue()).age.set(t.getNewValue());
    });
    ObservableList<Person> people2 = FXCollections.observableArrayList();
    for (int i = 0; i < 200; i++) people2.add(new Person(names[random.nextInt(names.length)], names[random.nextInt(names.length)], random.nextInt(100)));
    editableTreeTableView.setRoot(new RecursiveTreeItem<Person>(people2, RecursiveTreeObject::getChildren));
    editableTreeTableView.setShowRoot(false);
    editableTreeTableView.setEditable(true);
    editableTreeTableViewCount.textProperty().bind(Bindings.createStringBinding(() -> "( " + editableTreeTableView.getCurrentItemsCount() + " )", editableTreeTableView.currentItemsCountProperty()));
    searchField2.textProperty().addListener((o, oldVal, newVal) -> {
        editableTreeTableView.setPredicate(person -> person.getValue().firstName.get().contains(newVal) || person.getValue().lastName.get().contains(newVal) || (person.getValue().age.get() + "").contains(newVal));
    });
}
Also used : CellEditEvent(javafx.scene.control.TreeTableColumn.CellEditEvent) TextFieldEditorBuilder(com.jfoenix.controls.cells.editors.TextFieldEditorBuilder) IntegerTextFieldEditorBuilder(com.jfoenix.controls.cells.editors.IntegerTextFieldEditorBuilder) Random(java.util.Random) TreeTableColumn(javafx.scene.control.TreeTableColumn) IntegerTextFieldEditorBuilder(com.jfoenix.controls.cells.editors.IntegerTextFieldEditorBuilder) PostConstruct(javax.annotation.PostConstruct)

Aggregations

TextFieldEditorBuilder (com.jfoenix.controls.cells.editors.TextFieldEditorBuilder)2 TreeTableColumn (javafx.scene.control.TreeTableColumn)2 CellEditEvent (javafx.scene.control.TreeTableColumn.CellEditEvent)2 IntegerTextFieldEditorBuilder (com.jfoenix.controls.cells.editors.IntegerTextFieldEditorBuilder)1 RecursiveTreeObject (com.jfoenix.controls.datamodels.treetable.RecursiveTreeObject)1 Random (java.util.Random)1 Insets (javafx.geometry.Insets)1 Scene (javafx.scene.Scene)1 Label (javafx.scene.control.Label)1 FlowPane (javafx.scene.layout.FlowPane)1 PostConstruct (javax.annotation.PostConstruct)1