views:

536

answers:

1

I have 3 fields in my ui 2 buttons, one on top and other on buttom of a label field...when ever the label field gets focus i want the buttons to appear and i should be able to click on them...and when the label field loses focus the buttons should disappears..how can i do this...

+2  A: 

put some manager field as a placeholder at button position, then add a FocusChangeListener to label and use add/delete field on focusChanged to show/hide button.

UPDATE
Since every focus change from fields may change layout, think its better to add listener to every field added to screen and placeholder:

class Scr extends MainScreen {
    HorizontalFieldManager placeholder = new HorizontalFieldManager() {
        public void add(Field field) {
            if (field.getFocusListener() != null)
                field.setFocusListener(null);
            field.setFocusListener(focusListener);
            super.add(field);
        }
    };
    ButtonField buttonField = new ButtonField("button",
            ButtonField.CONSUME_CLICK);
    LabelField labelField = new LabelField("label", FOCUSABLE);

    public Scr() {
        add(placeholder);
        add(labelField);
        add(new LabelField("label2", FOCUSABLE));
    }

    public void add(Field field) {
        if (!(field instanceof Manager)) {
            if (field.getFocusListener() != null)
                field.setFocusListener(null);
            field.setFocusListener(focusListener);
        }
        super.add(field);
    }

    FocusChangeListener focusListener = new FocusChangeListener() {
        public void focusChanged(Field field, int eventType) {
            if (eventType == FOCUS_GAINED) {
                if (field == labelField) {
                    if (buttonField.getManager() == null)
                        placeholder.add(buttonField);
                } else if (field != buttonField)
                    placeholder.delete(buttonField);
            }
        }
    };
}
Max Gontar
i have one question....i have not tested this yet but if the label field has focus...and i try to click on the button..will the button not disappears...i want to be able to click the button as well...please correct me if i am wrong...thanks..
Kaddy
I suspect that will happen. It may work on touch devices, but on trackball devices the button will have to have focus before in can be clicked. I must confess that I am completely stumped as to what you are trying to achieve here.
Richard
this does not seem to work on 9500...i see another screen overlapping...
Kaddy