Fire event when switching stacks of StackLayoutPanel in GWT 2.0
I'm trying to catch an event, when I switch the stacks of a StackLayoutPanel in GWT 2.0.
The biggest problem is that I don't know which 开发者_开发知识库event gets fired and there seems to be no documentation, I have added a ChangeHandler with addDomHandler() but that hasn't worked. Unfortunately the StackLayoutPanel doesn't implement the getSelectedIndex()-function so I can't just use an ClickEvent and then check if the selected index has changed.Is my only solution to use the StackPanel or is there a way to get this to work with the StackLayoutPanel?
I believe in GWT 2.0.3 StackLayoutPanel has method addSelectionHandler. Fired event is SelectionEvent and event.getSelectedItem() returns corresponding stack header id.
What I did, after some experiments: use a Label as the widget of the header with 100% width and then add a ClickEvent handler attached. Every time the ClickEvent arrives, it means the user clicked the header, so the panel will be visible... Not very nice, but it worked. I tried to wrap the header widget using a ClickWrapper (take a look to the StackLayoutPanel source code), but it didn't worked, I dont know exactly why...
I did it this way
public class StackComponent extends StackLayoutPanel implements HasChangeHandlers {
public StackComponent(Unit unit) {
super(unit);
}
@Override
public void showWidget(Widget widget) {
super.showWidget(widget);
fireEvent(new StackChangeEvent(widget));
}
@Override
public HandlerRegistration addChangeHandler(ChangeHandler handler) {
return addDomHandler(handler, ChangeEvent.getType());
}
}
This worked form me:
private int selectedIndex = 0;
public Main() {
initWidget(ourUiBinder.createAndBindUi(this));
navigatorStackPanel.add(yourWidgetOne, "Title one");
navigatorStackPanel.add(yourWidgetTwo, "Title two");
navigatorStackPanel.addHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
if (navigatorStackPanel.getSelectedIndex() != selectedIndex) {
selectedIndex = navigatorStackPanel.getSelectedIndex();
if (selectedIndex == 0)
do something one;
else
do something two;
GWT.log(" Changed");
}
}
}, ClickEvent.getType());
}
精彩评论