Keyboard control for Tree does not work when container ds is dynamic

I’ve created a Tree component in which the contents of the tree are refreshed when the user expands a given node.

public class FileBrowserTree extends Tree implements ExpandListener {
	...
	public FileBrowserTree(String[] strRoot) {
		super();
		fileBrowser = new FileBrowserContainer(strRoot);
		this.setContainerDataSource(fileBrowser);
		this.setNullSelectionAllowed(false);
		this.addListener((ExpandListener)this);
		...
	}
	
	@Override
	public void nodeExpand(ExpandEvent event) {
		if(event.getItemId()!=null && event.getItemId() instanceof Integer) {
			[b]
fileBrowser.refreshContetnts((Integer)event.getItemId());
[/b] [i]
// this refreshes the container everytime a node is expanded
[/i]
		}
	}
	...
}

If I remove the highlighted code, keyboard navigation works absolutely fine.
Is this a bug or I need to handle it somewhere in my code.

Thanks in advance.

It is very obvious that we can add/edit/delete items in container data source.
I’ve observed a very strange behavior in Vaadin components when data in container is altered on user action.
Take this small example for demonstration:
I’ve created a table in which when user clicks on a row, a new row is appended to the table. Issues I observed here are:

  1. Key board navigation does not work.
    The focus gets lost when a new row is added.
    (Works to some extent in FF only)
  2. In the demo app, after some clicks the table will have a vertical scrollbar. Scroll to the bottom and click any item except the last item.
    The click event is not triggered now!!
package com.example.tabledemo;

import com.vaadin.Application;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Table;
import com.vaadin.ui.Window;

public class TabledemoApplication extends Application {
	private static final long serialVersionUID = 2019881869735819969L;
	private static final String NAME = "NAME";
	
	@Override
	public void init() {
		
		Window mainWindow = new Window("Table Demo Application");
		final Table table = new Table("demo table");
		table.setImmediate(true);
		table.setSelectable(true);
		table.setMultiSelect(false);
		IndexedContainer container = getDS();
		table.setContainerDataSource(container);
		table.addListener(new ItemClickListener() {
			private static final long serialVersionUID = -3858888802600927507L;

			@Override
			public void itemClick(ItemClickEvent event) {
				System.out.println(event.getItem()+" is clicked");
				Object itemId = table.addItem();
				Item item = table.getItem(itemId);
				if(item!=null && item.getItemProperty(NAME)!=null) {
						item.getItemProperty(NAME).setValue("New: "+itemId);
				}
			}
		});
		
		mainWindow.addComponent(table);
		setMainWindow(mainWindow);
	}

	private IndexedContainer getDS() {
		String[] names = new String[]
 {"Apple","Microsoft","Motorola","Nokia","Samsung","LG"};
		IndexedContainer container = new IndexedContainer();
		container.addContainerProperty(NAME, String.class, null);
		
		for(int i=0; i<names.length; i++) {
			Item item = container.addItem(i+1);
			item.getItemProperty(NAME).setValue(names[i]
);
		}
		return container;
	}
}

Can anyone explain to me this strange behavior?

Pranay,

I’m struggling with the same right now.

When responding to the Tree.Expand, through an Tree.Expand listener. And then programmatically adding some new child nodes. I can observe the following behavior.

  • The insertion part works flawlessly.

However, with the following problems / issues.

  • Keyboard input on the tree freezes, leaving the node that was selected when the expand was invoked selected.
  • Keyboard navigation works again after interacting with the tree with the mouse.

Observed with:

  • Safari 5.1 (OSX 10.7)
  • IE 9 (Win 7)
  • Chrome 13 (OSX 10.7, Ubuntu 10.10)
  • Firefox 3.6.18 (Ubuntu 10.10)
  • Firefox 6 (OSX 10.7)
  • Opera 11.50 (OSX 10.7)

Which covers all the browsers I have access to at the moment.

I have not tested or observed what you write about click events not being triggered.

Just wanted to show that you are not the only one with this problem. Did you find a solution? (I’m all ears.)

Though not a show stopper, it’s not cool in a live application, leaving me with the option of disabling keyboard input all together. If possible.

I can also confirm this behavior. Tested with Vaadin 6.6.5. Definitely a bug to be fixed as it can make Tree completely un-focusable with keyboard - or almost completely because refreshing page will help.

Looks like there is already a Trac issue created for this
#7341
.

Track issue is still with new status but fortunately it looks that Vaadin 6.7.0 fixes this problem.

Hi guys,

Yes, the issue is with focus being broken in Tree. And as Ari noted, this has been fixed in 6.7.0 in
#7057
.

HTH,
/Jonatan