Select all text in TextField componen when its get a focus

Hi,

How can I make all text in TextField component selected when it get focus or user enter into it?

Hi @haattila

To add new functionality, you need to extend the TextField component. Follow these steps:

  1. Create web-toolkit module;
  2. Extend CubaTextFieldWidget. Create CustomTextFieldWidget class in web.toolkit.ui.client.customtextfield package:
package com.company.sample.web.toolkit.ui.client.customtextfield;

import com.google.gwt.event.dom.client.FocusEvent;
import com.haulmont.cuba.web.widgets.client.textfield.CubaTextFieldWidget;

public class CustomTextFieldWidget extends CubaTextFieldWidget {
    @Override
    public void onFocus(FocusEvent event) {
        super.onFocus(event);
        selectAll();
    }
}
  1. Replace CubaTextFieldWidget with CustomTextFieldWidget in AppWidgetSet.gwt.xml
    AppWidgetSet.gwt.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<module>
    <inherits name="com.haulmont.cuba.web.widgets.WidgetSet"/>

    <replace-with class="com.company.sample.web.toolkit.ui.client.customtextfield.CustomTextFieldWidget" >
        <when-type-is class="com.haulmont.cuba.web.widgets.client.textfield.CubaTextFieldWidget"/>
    </replace-with>
</module>

I also attached the test project textfield-select-all.zip (76.4 KB).

Regards,
Gleb

1 Like