Bubolina
Bubolina

Reputation: 161

SmartGWT TextItem - focusInItem() method not working?


I have a search form that opens in a com.smartgwt.client.widgets.Window.Window(). In it, I have a VLayout, in which I have a search form:

DynamicForm search = new DynamicForm();
// setMargin, setTitle, setNumCols
TextItem name = new TextItem();
name.setFormatOnFocusChange(true);
//setEditorValueFormatter, etc.
search.setFields(/*some fields*/, name, /*other fields*/);
name.focusInItem();

And the focus is not in the item (it's nowhere). Why is that so?
Thank you in advance!
EDIT:
Here is the code of the two Mediators:

public class MainMediator extends Mediator {
private Window popup = new Window();

protected void initView(){
        // here I have a Form with fields and icon on one TextItem, on which I do:
    searchField.addIconClickHandler(new IconClickHandler() {
    popup = new Window();
    popup.setIsModal(true);
    popup.setShowModalMask(true);
    });
}

public final void handleNotification(final INotification notification){
    // if the right notification is sent, execute this code:
    PopupMediator m = (PopupMediator) this.getFacade().retreiveMediator(PopupMediator.NAME);
        VLayout popupLayout = (VLayout) m.getViewComponent();
    popup.addItem(popupLayout);
        popup.show();
    } 
}

public class PopupMediator extends Mediator {
    protected void initView(){
    viewComponent = new VLayout();
    DynamicForm searchForm = new DynamicForm();
    // searchForm props
    TextItem name = new TextItem();
    // name props and some other fields
    searchForm.setFields(name /* and the others */);
        VLayout searchFormContainer = new VLayout();
    // searchFormContainer props
        searchFormContainer.setMembers(seachForm);
        name.focusInItem(); // not working on popup shown
    HLayout searchContainer = new HLayout();
    // searchContainer props
    searchContainer.setMembers(grid1, searchFormContainer);
    VLayout container = new VLayout();
    // container props
        container.setMembers (searchContainer, grid2);
        ((VLayout)viewComponent).setMembers(container, buttons);
}

Upvotes: 1

Views: 2703

Answers (3)

Sithsu
Sithsu

Reputation: 2219

Not sure how you receive handleNotification() callbacks, but you shouldn't use window.addItem() in it.
That will cause multiple items to be added/overwritten each time callback is called.

If handleNotification() callback is required, it should be only used for window.show(), plus any form field population/setting focus/etc.

If the content of Window is NOT going to change from one callback to another, initialize window layout during window creation.
If content of Window is GOING to change from one callback to another, you will need to remove previously added items.

Here's a simple working implementation that popup the window on a button click and set focus on a given field.

TextItem name1 = new TextItem("name1", "Name 1");
final TextItem name2 = new TextItem("name2", "Name 2"); // setting focus to name2
TextItem name3 = new TextItem("name3", "Name 3");

final DynamicForm searchForm = new DynamicForm();
// searchForm.setAutoFocus(true); // sets focus to first focusable field
searchForm.setFields(name1, name2, name3);

VLayout searchFormContainer = new VLayout();
searchFormContainer.setMembers(searchForm);

final Window window = new Window();
window.setIsModal(true);
window.setShowModalMask(true);
window.setAutoCenter(true);
window.setSize("400px", "300px");
window.addItem(searchFormContainer);

Button button = new Button("Search");
button.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
        window.show();
        name2.focusInItem();
       // searchForm.focusInItem(name2); // this also works
    }
});

Its possible to use DynamicForm.setAutoFocus to automatically focus on first focusable field in the form.

Upvotes: 1

RAS
RAS

Reputation: 8158

You're getting this problem because formitem.focusInItem() works only after the formitem is drawn or say rendered in the browser. Adding the formitem in DynamicForm does not draw it.

I don't know where you're placing the DynamicForm, but to understand it completely, look at the following code:

Window window = new Window();
window.setSize("900px", "500px");
VLayout layout = new VLayout();
DynamicForm dynamicForm = new DynamicForm();
dynamicForm.setSize("800px", "400px");
TextItem item = new TextItem();
dynamicForm.setFields(item);
item.focusInItem(); // This won't work.
layout.addMember(dynamicForm);
window.addItem(layout);
item.focusInItem(); // This won't work.
window.show();
item.focusInItem(); // This will work.

So change your code accordingly.

Upvotes: 4

user2468261
user2468261

Reputation:

Why don't you try to focus on the form itself:

search.focus();

Upvotes: 0

Related Questions