Problem with changing MVVM tree model

asked 2013-05-08 21:06:53 +0800

davout gravatar image davout
1435 3 18

I'm using a Tree with a MVVM form to display a two level hierarchy of data, like:

- Category 1
  |--- Sub Category 1-1
- Category 2
  |--- Sub Category 2-1
  |--- Sub Category 2-2
 - Category 3
  |--- Sub Category 3-1

I've developed by own custom model and node classes to support this tree. The tree supports drag'n drop of items and the form has buttons for moving 'category' items up and down in the order.

However, when I try to move a category item that is not at the top to the top position I'm getting an error.

Here's the method that handles the 'move to top' button event handler...

public void moveCategoryTop() {
    // find the root node from the tree model
    EisTreeModelNode aRoot = getCategories().getRoot();
    // find the currently selected node
    EisTreeModelNode aSelectedNode = getSelectedCategory();
    // remove the selected node from the root child list
    // add the selected node back in as the first child of the root
    aRoot.addChildNode(0, aSelectedNode);
    // setup all top level nodes so that are open
    // change selection to make it the new top category
    int[] aSelectionPath = {0};

After completing the method the app throws an invalid index error inside my model - asking for a child node at an index position outside of what the parent node has.

It's as though after the drag'n'drop action has been completed the tree is trying to repaint itself using the model state BEFORE the drop action code shown above was executed.

Any ideas?

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted

answered 2013-05-09 06:12:48 +0800

davout gravatar image davout
1435 3 18

Worth adding that the Tree has 'checkmark="true"'in play and as I'm dragging the active/selected item I'm wondering if the tree isn't trying to repaint itself before it reacts to a change in the model.

link publish delete flag offensive edit

answered 2013-05-20 06:14:48 +0800

benbai gravatar image benbai
2228 6

Regarding to move ndoe that is not at the top to the top, the code below works fine with ZK


    <window apply="org.zkoss.bind.BindComposer"
        viewModel="@id('vm') @init('test.TestVM')">
        <tree model="@bind(vm.model)"
            width="600px" height="200px"
                <treecol label="data" />
            <template name="model" var="node">
                        <treecell label="@bind(node.data)" />
        <button label="move to top" onClick="@command('moveToTop')" />


package test;

import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.event.SelectEvent;
import org.zkoss.zul.DefaultTreeModel;
import org.zkoss.zul.DefaultTreeNode;
import org.zkoss.zul.Tree;
import org.zkoss.zul.TreeModel;

 * tested with ZK
 * @author benbai
@SuppressWarnings({ "rawtypes", "unchecked" })
public class TestVM {
    DefaultTreeModel _model;
    DefaultTreeNode _selectedNode;

    public TreeModel<DefaultTreeNode<String>> getModel () {
        DefaultTreeNode root = new DefaultTreeNode(null,
                new DefaultTreeNode[] {
                    new DefaultTreeNode("aaa"),
                    new DefaultTreeNode("bbb", new DefaultTreeNode[] {new DefaultTreeNode("ddd")}),
                    new DefaultTreeNode("ccc")
        if (_model == null) {
            _model = new DefaultTreeModel<String>(root);
        return _model;

    public void keepSelectedNode (@ContextParam(ContextType.TRIGGER_EVENT) SelectEvent event) {
        _selectedNode = ((Tree)event.getTarget()).getSelectedItem().getValue();
    public void moveToTop () {
        if (_selectedNode != null) {
            DefaultTreeNode root = (DefaultTreeNode)_selectedNode.getModel().getRoot();
            root.insert(_selectedNode, 0);

Regarding drag'n'drop issue, could you provide your scenario more details?

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

1 follower



Asked: 2013-05-08 21:06:53 +0800

Seen: 79 times

Last updated: May 20 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More