我编写了以下代码来打印一棵树:
package test.com.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class TestGwt implements EntryPoint
{
public static String bc="";
public void onModuleLoad()
{
VerticalPanel v = new VerticalPanel();
TreeItem a = new TreeItem( "A" );
TreeItem b = new TreeItem( "B" );
TreeItem c = new TreeItem( "C" );
TreeItem d = new TreeItem( "D" );
TreeItem f1 = new TreeItem( "f1" );
TreeItem f11 = new TreeItem( "f11" );
TreeItem f12 = new TreeItem( "f12" );
d.addItem( f1 );
f1.addItem( f11 );
f1.addItem( f12 );
final Tree t = new Tree();
t.addItem( a );
t.addItem( b );
t.addItem( c );
t.addItem( d );
t.addSelectionHandler(new SelectionHandler<TreeItem>()
{
@Override
public void onSelection(SelectionEvent<TreeItem> event)
{
TreeItem selectedItem= event.getSelectedItem();
if(selectedItem != null)
{
System.out.println("Selected Item: "+ selectedItem.getText());
TreeItem parentItem = event.getSelectedItem().getParentItem();
t.setSelectedItem(parentItem);
parentItem=parentItem.getParentItem();
}
}
});
v.add(t);
// Add it to the root panel.
RootPanel.get().add(v);
}
}
节点 D 有子节点 f1。f1 有子节点 f11 和 f12。如果用户选择 c f12,它应该打印:
D>f1>f12。
当所选项目变为 D 时,我的代码会崩溃,因为 D 没有父项。如何改进此代码,以便能够根据用户选择的节点带来 bradcrumb?
有什么方法可以检查所选节点是否是根节点?
非常感谢。-玛丽亚
更新的工作代码:
package test.com.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class TestGwt implements EntryPoint
{
public static String bc="";
public void onModuleLoad()
{
VerticalPanel v = new VerticalPanel();
TreeItem a = new TreeItem( "A" );
TreeItem b = new TreeItem( "B" );
TreeItem c = new TreeItem( "C" );
TreeItem d = new TreeItem( "D" );
TreeItem f1 = new TreeItem( "f1" );
TreeItem f11 = new TreeItem( "f11" );
TreeItem f12 = new TreeItem( "f12" );
d.addItem( f1 );
f1.addItem( f11 );
f1.addItem( f12 );
final Tree t = new Tree();
t.addItem( a );
t.addItem( b );
t.addItem( c );
t.addItem( d );
t.addSelectionHandler(new SelectionHandler<TreeItem>()
{
@Override
public void onSelection(SelectionEvent<TreeItem> event)
{
TreeItem pItem = event.getSelectedItem().getParentItem();
if(pItem == null)
{
bc+=" > "+event.getSelectedItem().getText();
}
else
{
TreeItem selectedItem= event.getSelectedItem();
if(selectedItem != null)
{
bc+=" > "+ selectedItem.getText();
TreeItem parentItem = event.getSelectedItem().getParentItem();
t.setSelectedItem(parentItem);
parentItem=parentItem.getParentItem();
}
}
}
});
System.out.println(bc);
v.add(t);
// Add it to the root panel.
RootPanel.get().add(v);
}
}