Skip to main content

textfield focus traversal

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
5 replies [Last post]
tocomo
Offline
Joined: 2004-11-10

How can i prevent left/right focus cycling within TextFields?

public class TextForm extends Form {
    public TextForm() {
        super();
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        addComponent(new TextField());
        addComponent(new TextField());
        addComponent(new TextField());
    }
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vprise
Offline
Joined: 2003-11-07

Try TextField.setLeftAndRightEditingTrigger()

tocomo
Offline
Joined: 2004-11-10

thanks for taking time. But this has the side effect, that game_right navigation moves focus down!
what i try to achieve is:

  • stop focus down traversal on last element in list AND set back upwards selecting last list element.
  • no right and left navigation with the result of jumping components down.

Can i do that? And how?

here my full test example:

package samples;

import com.sun.lwuit.Component;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.TextField;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.list.ListCellRenderer;

public class ListForm extends Form {

public ListForm() {
super();
String[] elements = new String[5];
for (int i = 0; i < elements.length; i++) {
elements[i] = "" + i;
}
setLayout(new BorderLayout());
TextField cmp = new TextField();
addComponent(BorderLayout.NORTH, cmp);
addComponent(BorderLayout.CENTER,
createList(elements, new TestRenderer()));
setCyclicFocus(false);
}

private List createList(String[] elements, ListCellRenderer renderer) {
List list = new List(elements);
list.setSmoothScrolling(false);
list.setRenderer(renderer);
return list;
}

static class TestRenderer extends Label implements ListCellRenderer {

private static final int FG_COLOR = 0xffffff;
private static final int BG_COLOR_SELECTED = 0x0000ff;
private static final int BG_COLOR_UNSELECTED = 0x000000;

public TestRenderer() {
getStyle().setFgColor(FG_COLOR);
setSmoothScrolling(false);
}

public Component getListCellRendererComponent(List list, Object value,
int index, boolean isSelected) {
if (isSelected) {
getStyle().setBgColor(BG_COLOR_SELECTED);
} else {
getStyle().setBgColor(BG_COLOR_UNSELECTED);
}
// order matters: size becomes recalculated on setText
setText((String) value);
setPreferredW(Display.getInstance().getDisplayWidth());
return this;
}

public Component getListFocusComponent(List list) {
return null;
}
}
}
vprise
Offline
Joined: 2003-11-07

Use setNextFocusLeft/Right methods on the text field to determine focus destination.

tocomo
Offline
Joined: 2004-11-10

setNextFocusLeft/Right can not be null. So it can not become disabled.

vprise
Offline
Joined: 2003-11-07

It seems I don't understand what you are trying to accomplish?
Can you please illustrate it clearly?
Notice that you can derive and override most of the text field methods to achieve pretty much any behavior you might want.