I would like to select a TextItem via textItem.selectValue ().
Documentation says "Put focus in this item and select the entire value. Only applies to text based items", which is what I want.
However, I could not get it to work.
I tried several methods: see the code below where I have marked "Test #1 - Test #6", along with comments as to what the results for that test where.
The only way I could get selectValue () to work is either in response to some user action (like a button click), or to execute it in "delayed" fashion via a Timer, which hopefully runs after the form is drawn...
Any ideas?
1. the SmartGWT or SmartClient version and browser version(s) involved;
SmartGWT 2.4
GWT 2.1.1
Chrome 8.0.552.237
IE 9.0.7930.16406IS
2. for a server-side problem, the complete logs generated during processing of the request;
N/A
3. for a client-side problem, the contents of the Developer Console (see FAQ for usage);
4. if there is a JavaScript error, the stack trace logged in the Developer Console (from Internet Explorer if possible); and
N/A
5. sample code.
Documentation says "Put focus in this item and select the entire value. Only applies to text based items", which is what I want.
However, I could not get it to work.
I tried several methods: see the code below where I have marked "Test #1 - Test #6", along with comments as to what the results for that test where.
The only way I could get selectValue () to work is either in response to some user action (like a button click), or to execute it in "delayed" fashion via a Timer, which hopefully runs after the form is drawn...
Any ideas?
1. the SmartGWT or SmartClient version and browser version(s) involved;
SmartGWT 2.4
GWT 2.1.1
Chrome 8.0.552.237
IE 9.0.7930.16406IS
2. for a server-side problem, the complete logs generated during processing of the request;
N/A
3. for a client-side problem, the contents of the Developer Console (see FAQ for usage);
Code:
Test #1 09:09:37.326:INFO:Log:initialized 09:09:37.328:WARN:Page:NOTE: isc.Page.getWidth() called before <BODY> tag was written out -- value cannot be determined. Returning 500 09:09:37.328:WARN:Page:NOTE: isc.Page.getHeight() called before <BODY> tag was written out -- value cannot be determined. Returning 500 09:09:37.382:WARN:AutoObserver:Use addInterfaceProperties() to add methods to interface [Class AutoObserver] 09:09:37.470:INFO:Log:isc.Page is loaded Test #3 09:10:41.993:INFO:Log:initialized 09:10:41.995:WARN:Page:NOTE: isc.Page.getWidth() called before <BODY> tag was written out -- value cannot be determined. Returning 500 09:10:41.995:WARN:Page:NOTE: isc.Page.getHeight() called before <BODY> tag was written out -- value cannot be determined. Returning 500 09:10:42.050:WARN:AutoObserver:Use addInterfaceProperties() to add methods to interface [Class AutoObserver] 09:10:42.138:INFO:Log:isc.Page is loaded Test #5 09:11:09.105:INFO:Log:initialized 09:11:09.107:WARN:Page:NOTE: isc.Page.getWidth() called before <BODY> tag was written out -- value cannot be determined. Returning 500 09:11:09.107:WARN:Page:NOTE: isc.Page.getHeight() called before <BODY> tag was written out -- value cannot be determined. Returning 500 09:11:09.161:WARN:AutoObserver:Use addInterfaceProperties() to add methods to interface [Class AutoObserver] 09:11:09.250:INFO:Log:isc.Page is loaded Test #6 09:12:05.592:INFO:Log:initialized 09:12:05.734:WARN:AutoObserver:Use addInterfaceProperties() to add methods to interface [Class AutoObserver] 09:12:05.869:INFO:Log:isc.Page is loaded
N/A
5. sample code.
Code:
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.util.SC;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
public class TestDynamicFormFocusInItem implements EntryPoint {
// my attributes
final DynamicForm form = new DynamicForm ();
final TextItem userNameFormItem = new TextItem ();
final TextItem addressFormItem = new TextItem ();
/**
* The EntryPoint interface
*/
public void onModuleLoad () {
// show dev console
SC.showConsole ();
// create form items
userNameFormItem.setTitle ("Username");
userNameFormItem.setValue ("shortpasta");
addressFormItem.setTitle ("Address");
addressFormItem.setValue ("shortpasta.org");
final ButtonItem focusUsernameButtonItem = new ButtonItem ();
focusUsernameButtonItem.setTitle ("Focus Username");
focusUsernameButtonItem.addClickHandler (new ClickHandler () {
public void onClick (ClickEvent clickEvent) {
// Test #1: perform the selection on button click
// Chrome 8.0.552.237 : value is selected, cursor not visible
// IE 9.0.7930.16406IS: value is NOT selected, cursor placed at the beginning of the text
userNameFormItem.selectValue ();
}
});
final ButtonItem focusAddressButtonItem = new ButtonItem ();
focusAddressButtonItem.setTitle ("Focus Address");
focusAddressButtonItem.addClickHandler (new ClickHandler () {
public void onClick (ClickEvent clickEvent) {
addressFormItem.selectValue ();
}
});
// load form
form.setFields (
userNameFormItem,
addressFormItem,
focusUsernameButtonItem,
focusAddressButtonItem);
// Test #2: perform the selection after initializing the form
// Fails with com.google.gwt.core.client.JavaScriptException: (TypeError): Object # has no method 'selectValue'
// Too early?
// addressFormItem.selectValue ();
// Test #3: perform the selection when the form is drawn
// Chrome 8.0.552.237 : value is NOT selected, cursor placed at the end of the text
// IE 9.0.7930.16406IS: value is NOT selected, cursor placed at the end of the text
// reason: this method is not called
/*form.addDrawHandler (new DrawHandler () {
public void onDraw (final DrawEvent drawEvent) {
GWT.log ("form.onDraw ()");
addressFormItem.selectValue ();
}
});*/
// display
final VLayout layout = new VLayout ();
layout.setWidth100 ();
layout.setHeight100 ();
layout.setLayoutMargin (5);
layout.addMember (form);
// Test #4: perform the selection when the form parent is drawn
// reason: this method is not called
/*layout.addDrawHandler (new DrawHandler () {
public void onDraw (final DrawEvent drawEvent) {
GWT.log ("layout.onDraw ()");
addressFormItem.selectValue ();
}
});*/
// display
GWT.log ("calling layout.draw ()");
layout.draw ();
GWT.log ("calling layout.draw (): done");
// Test #5: perform the selection after the parent is drawn
// Chrome 8.0.552.237 : value is NOT selected, cursor placed at the end of the text
// IE 9.0.7930.16406IS: value is NOT selected, cursor placed at the end of the text
// addressFormItem.selectValue ();
// Test #6: perform the selection through a timer
// Chrome 8.0.552.237 : value is selected, cursor not visible
// IE 9.0.7930.16406IS: value is NOT selected, cursor placed at the beginning of the text
createSelectValueTimer ();
}
/**
* Creates a Timer to perform the selection in 100 msecs
*/
private void createSelectValueTimer () {
final int basePeriod = 100;
final Timer timer = new Timer () {
@Override
public void run () {
// handle exceptions
try {
GWT.log ("Timer.run (): selecting address");
addressFormItem.selectValue ();
}
catch (final Exception e) {
GWT.log (null, e);
}
}
};
timer.schedule (basePeriod);
}
}
Comment