PDA

View Full Version : Set Focus on a TextField within a FormPanel



alessalessio
8 Apr 2011, 7:07 AM
I actually decides to open a thread about this argument, since i experienced it is not working as i imagined,and read many post about this but found no clear solution.

The problem statement is quite simple:

Have a form panel and Fields within it, how can i set the focus onto a field?

The next two simple code does not do the job:

1)
TextField tf = new TextField<String>();
Button button = new Button("focus on textfield");

button.addSelectionListener(new SelectionListener<ButtonEvent>(){
@Override
public void componentSelected(ButtonEvent ce) {
tf.setFocus();
}
});


2)
TextField tf = new TextField<String>();
Window window = new Window();
window.add(tf);

window.setFocusWidget(tf);



What am i doing wrong?

liondev
8 Apr 2011, 12:49 PM
if you textfield is into a panel just:

component.focus();

if your component is on a Window use

setFocusWidget(component);

alessalessio
11 Apr 2011, 5:41 AM
liondev, thanks for the reply,
but if you notice the piece of code i wrote,
i used both commands, and it doesn't work! That's the problem :D





if you textfield is into a panel just:

component.focus();

if your component is on a Window use

setFocusWidget(component);

liondev
11 Apr 2011, 8:12 AM
try: make your own class extends from Window or ContenPanel

in the constructor add the components u will need.
then focus it


that works for me