13 Replies Latest reply on Jan 30, 2012 1:31 PM by 543313

    TreeView / TreeItem value changes not visible

    543313
      I have a application with on one side a tree and on the other side a gridpane (representing a selected value from the treeview). The gridpane is populated when a right click event is triggered on the tree (thus instantiating the pane and populating it with the data)

      The problem
      TreeItem.getValue() values change, but the corresponding display value in the TreeView does not change.
      The value is changed in a GridPane with textfields. When this value is edited for the second time, the changed value is shown in the gridpane. Thus verifying, that the value has changed.

      Question.
      How can the display value (in the TreeView) be refreshed when the underlying value (TreeItem.getValue()) changes?
        • 1. Re: TreeView / TreeItem value changes not visible
          zonski
          Simplest option is to just force the TreeView to update by making the 'items' list fire an event. You can do this by removing and re-adding your item in the list (or by setting a 'new' copy of your item into the list).

          Alternatively, assuming something is firing an event when you change the 'value' then you can write a custom CellFactory to return a custom TeeCell. In this TreeCell listen for the events and then update the TreeCell (i.e. call setText) in response to any changes.

          If your 'value' is observable (i.e. it is a bean with Observable properties on it that you can listen to, etc) then it can be the source of your events, and you can just add a listener to it in your upateItem method (making sure you remove your listener from the previous item if there was one). Otherwise you may need to create some sort of event channel that your TreeCell listens to, then when you update your 'value' fire an event on the event channel to trigger an update.

          See how you go with that, if you need more info, post some details about your 'value' and how it gets 'changed'.
          1 person found this helpful
          • 2. Re: TreeView / TreeItem value changes not visible
            Jonathan Giles-Oracle
            TreeView already updates itself when the visible TreeItem values are updated. I just verified it locally by writing a simple test application that shows a TreeView, and has a button to its side that, when clicked, changes the value of the an element. Once the button is clicked, I immediately see the new value shown on screen.

            If you aren't seeing this, can you confirm that the TreeItem value is changing by adding a debug ChangeListener to the TreeItem.valueProperty()? If you don't see any output then the TreeItem is not updating. If you do see output, I'm wondering if perhaps you've set a custom cell factory on your TreeView?
            1 person found this helpful
            • 3. Re: TreeView / TreeItem value changes not visible
              543313
              I am indeed using a custom cell factory. I will try to use all the input here and try and find out more as to why the cell is not updating in the tree.

              Marc
              • 4. Re: TreeView / TreeItem value changes not visible
                543313
                I have tested my tree with the changelistener and Iam not seesing any change events being fired. Also I use a custom TreeCell.

                But when I close a branch containing my leaf and open it again. The changes are visible.

                Isn't there a method just telling the current treeitem to rerender itself.. Or tell the TreeItem parent to rerender itself. I find it just plain silly that there is no fireevent() method on treeItem. BIGGG booboo :)
                • 5. Re: TreeView / TreeItem value changes not visible
                  543313
                  Found a solution. It looks horrible, but it works.


                                      int index = currentTreeItem.getParent().getChildren()
                                                .indexOf(currentTreeItem);
                                      currentTreeItem.getParent().getChildren()
                                                .set(index, currentTreeItem);


                  This will force the tree to update the tree labels. But I still find it silly that there is no easy way to do the following:

                  currentTreeItem.fireEvent(new Event(treeChangedEvent));

                  or

                  currentTreeItem.getParent.fireEvent(new Event(treeChangedEvent));


                  That is how it supposed to be working. Bubbling can take over from there.
                  • 6. Re: TreeView / TreeItem value changes not visible
                    Richard Bair-Oracle
                    Do you want to file an RFE?

                    Thanks!
                    Richard
                    • 7. Re: TreeView / TreeItem value changes not visible
                      543313
                      Off-course I would like to do that. I have worked with a lot of technologies (like Flex/actonscript, javascript, swing, plain event driven java etc) where this is a "natural" feature in the component structure (ie. fireEvent on TreeItem that bubbles up and down again)

                      But I do not know where to begin here.
                      • 8. Re: TreeView / TreeItem value changes not visible
                        jsmith
                        But I do not know where to begin here.
                        http://javafx-jira.kenai.com (anybody can sign up and post issues or feature requests) post your feature request against the Runtime project with a controls label.
                        • 9. Re: TreeView / TreeItem value changes not visible
                          Jonathan Giles-Oracle
                          Rather than a controls 'label', set 'controls' as the component, and set 'TreeView' as the label.

                          Thanks,
                          -- Jonathan
                          • 10. Re: TreeView / TreeItem value changes not visible
                            543313
                            Hmm. That is a bit cryptic for me to understand it. Could you explain it a little more in my terms?

                            My objects in question are

                            TypeCell: (custom cell) which holds a Type-object with an attribute "label" that is displayed in the tree. THis is used in the cellfactory for the TreeView
                            TreeView: root (that is a Type-object)
                            TreeItem(s): are branches and leafs. A treeItem contains a Type-object

                            Kind regards,

                            Marc
                            • 11. Re: TreeView / TreeItem value changes not visible
                              Jonathan Giles-Oracle
                              In your custom cell, you will need to bind the textProperty() to something from your data model. Then, when your data model changes, the tree cell text will be updated. If you post a code sample I am happy to help.

                              -- Jonathan
                              • 12. Re: TreeView / TreeItem value changes not visible
                                Jonathan Giles-Oracle
                                You could also consider calling code along the following lines - but I haven't tested it locally as I don't have your code to test with:
                                TreeItem<String> root = new TreeItem<String("root");
                                String newValue = "test";
                                Event.fireEvent(root, new TreeItem.TreeModificationEvent<String>(TreeItem.valueChangedEvent(), root, newValue));
                                In short, this code compiles because TreeItem is an EventTarget, which means that whilst it is not a Node, it does support event bubbling up the TreeItem parent hierarchy, and these events are listened to by TreeView and the underlying skins. You'll note there are a number of EventTypes in TreeItem. Of course, in this example I use the 'root' TreeItem, but this need not be the case - fire the event on the relevant TreeItem, not the root TreeItem.

                                Let me know how you get on...

                                -- Jonathan
                                • 13. Re: TreeView / TreeItem value changes not visible
                                  543313
                                  This method you describe, works. I had no idea this was possible. This is more in line with the whole event mechanism.

                                  Your other method (binding textProperty())

                                  Here is the impl of my custom cell

                                  public class TypeCell extends TreeCell<Type> {

                                       public TypeCell() {
                                            super();
                                       }

                                       @Override
                                       protected void updateItem(Type item, boolean empty) {
                                            // calling super here is very important - don't skip this!
                                            super.updateItem(item, empty);

                                            setText(item == null ? "" : item.label);

                                       }

                                  }

                                  Is this enough info for you to show me the textProperty() idea?

                                  Kind regards,

                                  Marc