PDA

View Full Version : can't set focus in text field placed in TabItem when tab is selected



miroslaw
13 Apr 2011, 10:56 PM
I have a window with a TabPanel and 2 TabItems. In the second TabItem I have some text fields and I want that when the second TabItem is selected to place the focus in a first text field.

I Listen to the "show" event on the TabItem, but the focus is set only first time (if tab is selected) .
If I toggle tabs and select the secondary tab again the focus doesn't work again.

Please help why i can set the focus on TextField in TabItem, more than once!

The source is:
public void onModuleLoad() {
LayoutContainer lc = new LayoutContainer();
lc.setLayout( new FlowLayout());

final TabItem ti1 = new TabItem("Tab 1");
final TabItem ti2 = new TabItem("Tab 2");

final TextField<String> tf1 = new TextField<String>();
ti1.add(tf1);
final TextField<String> tf2 = new TextField<String>();
ti1.add(tf2);

final TextField<String> tf3 = new TextField<String>();
ti2.add(tf3);
final TextField<String> tf4 = new TextField<String>();
ti2.add(tf4);

final TabPanel tp = new TabPanel();
tp.add(ti1);
tp.add(ti2);

ti1.addListener(Events.Show, new Listener<BaseEvent>() {
@Override
public void handleEvent(BaseEvent be) {
tf1.focus();
}
});

ti2.addListener(Events.Show, new Listener<BaseEvent>() {
@Override
public void handleEvent(BaseEvent be) {
tf4.focus();
}
});

tp.setSize(300,300);

lc.add(tp);

RootPanel.get().add(lc);

StateManager.get().set(GWT.getModuleBaseURL() + "theme", "");
}