Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Force TreeView cell refresh

rmptDec 10 2012 — edited Dec 13 2012
I have a TreeView filled with an `ObservableList` of Local objects. When one of the Locals is selected in the TreeView, a TextField allow the user to edit the name of the Local. When the name attribute is changed I want to refresh the TreeView with the new value but I can't find out how!
I already read some solutions to bind a String from an Object to a Label/TextField (through `SimpleStringProperty`), but my problem is more complex, I want to bind an attribute from an object contained on a list to an element/cell of a TreeView.

I give you my source code so you can see my problem.


Local.java
    public class Local {
                
        private int         id;
        private String      name;
            
        public Local() {
            this.id             = -1;
            this.name           = "";
        }
                
        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id= id;
        }

        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
LocalController.java
    ...

    @FXML TreeView locals_tree_view;
    @FXML TextField name;

    ...

    // POPULATE TREE VIEW
    List<Local>           locals           = Database.getLocals();
    TreeItem<Family> rootNode     = new TreeItem<>(new Local());
    buildFamilyTree(rootNode, locals);
    tree_families.setRoot(rootNode);
    local_tree_view.setCellFactory(new Callback() {
            @Override
            public Object call(Object p) {
                return new LocalTreeCell ();
            }
        });
    ...

    // HANDLE NAME CHANGE
    name.textProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> ov, String t, String newText) {
            TreeItem<Local> selectedItem = (TreeItem) local_tree_view.getSelectionModel().getSelectedItem();

            Local selectedLocal = selectedItem.getValue();
            selectedLocal .setName(newText);

            // ******** UPDATE TREE CELL SOMEHOW ********
        }
    });
...
LocalTreeCell
        private final class LocalTreeCell extends TreeCell<Local> {
    
            @Override
            protected void updateItem(Local local, boolean empty) {
                super.updateItem(local, empty);
    
                if(empty) {
                    setGraphic(null);
                    setText(null);
                    return;
                }
                setText(local.getName());
            }
        }
 
Edited by: 976034 on 11/Dez/2012 5:59
FIxed error in my code, TreeView doesn't define a setItems method.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 10 2013
Added on Dec 10 2012
6 comments
3,170 views