Tree selection

Hello!
I have a problem with tree, i have one (for ex) with 3 elements:
-element1
-element2
-element3

I need so that if user clicks element2 or element3 the focus (highlighting in client side) come to element1. I tried to override ItemClickListener, and it works, but unfortunately partially: on the server side tree.isSelected() says that element1 is chosen (while user has clicked element2 or element3), while in client side clicked element is highlighted.
Thanks for help in advance!

I think it is easy to reach:

        final Tree tree = new Tree();
        tree.setSelectable(true);
        tree.setMultiSelect(false);
        tree.setImmediate(true); 
        tree.addItem("element1");
        tree.addItem("element2");
        tree.addItem("element3");
        tree.addItemClickListener(new ItemClickEvent.ItemClickListener() {
            
            @Override
            public void itemClick(ItemClickEvent event) {
                Object itemId = event.getItemId();
                if( "element2".equals(itemId) || "element3".equals(itemId) ) {
                    tree.setValue("element1");
                }
            }
        });

Thank you for your reply!
But the effect is the same as with tree.select()…i.e. IN server-side i’v got what I want (element1) but on the client side(in browser) element1 is not highlighted (what just I click remains highlighted)…

Sorry. I have tested it on Vaadin 7.3.3. It works as expected. What about your Vaadin version?

7.2.6

Yep, on 7.2.6 my code does not work, but the following works:

        final Tree tree = new Tree();
        tree.setSelectable(true);
        tree.setMultiSelect(false);
        tree.setImmediate(true);
        tree.addItem("element1");
        tree.addItem("element2");
        tree.addItem("element3");
        tree.addValueChangeListener(new Property.ValueChangeListener() {
            
            @Override
            public void valueChange(ValueChangeEvent event) {
                Object itemId = event.getProperty().getValue();
                if( "element2".equals(itemId) || "element3".equals(itemId) ) {
                    tree.setValue("element1");
                }
            }
        });

Thank you.
But I have another similiar issue:

[code]
final Tree tree = new Tree();

    tree.setImmediate(true);
    tree.addItemClickListener(new ItemClickListener(){
        @Override
        public void itemClick(ItemClickEvent event) {
            // TODO Auto-generated method stub                
            tree.removeItem(tree.getValue());
            tree.select(event.getItemId());                
        }
    });        
    tree.addItem("first");
    tree.addItem("second");
    tree.addItem("third");
    
    tree.select("first");

[/code]What expected? At the beginning we have first selected (highlighted), then we click “second” and expect that “first” is removed (OK) and second is selected (highlighted) (which we clicked). But no selection occurs. What I am doing wrong?

It is not obvious, but your last problem can be solved with call:

tree.setNullSelectionAllowed(false); The full peace of code:

final Tree tree = new Tree();
        tree.setSizeFull();
        tree.setSelectable(true);
        tree.setNullSelectionAllowed(false);
        tree.setNewItemsAllowed(false);
        tree.setMultiSelect(false);
        tree.setImmediate(true);
        tree.addItem("first");
        tree.addItem("second");
        tree.addItem("third");
        tree.addItemClickListener(new ItemClickEvent.ItemClickListener() {
            
            @Override
            public void itemClick(ItemClickEvent event) {
                Object itemId = event.getItemId();
                tree.removeItem(tree.getValue());
                tree.select(itemId);
            }
        });
        tree.select("first");

As for me you found bug and you can send the bug report. The problem is reproduced on 7.2.6 - 7.3.4.