Component Action Capture

62 views
Skip to first unread message

Muchey

unread,
Aug 16, 2018, 12:10:23 PM8/16/18
to CodenameOne Discussions
Am excited about the TextComponent, however, I have this challenge.

I am trying to attach an action event to the textfield in the textcomponent added uisng the line below, but I realise the TextComponent is a component, an receives other events except for the action event. This is just a container.

How do I capture the TEXTFIELD attached to the textcompnent to write in an actionEvent on it? I dont find using the getComponentAt() function quite friendly for this.

Any help/link greatly appreciated urgently.

Shai Almog

unread,
Aug 16, 2018, 11:29:25 PM8/16/18
to CodenameOne Discussions
Great.
Use getField() which returns the reference to the underlying text field.

Muchey

unread,
Aug 17, 2018, 9:07:46 AM8/17/18
to CodenameOne Discussions
Thanks Shai,

However, I realiased that the plugged event applied to the label and not the textfield in the TextComponent.

I need to attach this event to the TextField and not the label.

Please assist nudge me to right direction.

TextComponent partNo=new TextComponent().label("Part No.");
        partNo.getField().addActionListener((e) ->{
            //validate the Part No for the car.
        });

Shai Almog

unread,
Aug 18, 2018, 1:13:34 AM8/18/18
to CodenameOne Discussions
No this applies to the text field not the label part.
Notice that you can also use the validation framework to validate the text field. See the validator code here: https://www.codenameone.com/blog/pixel-perfect-text-input-part-2.html

Muchey

unread,
Aug 20, 2018, 8:20:06 AM8/20/18
to CodenameOne Discussions
Am here again.

This code snippet brought me back.
A mouse lick on the label executes the actionEvent, however several clicks on the textfield does nothing.

I need the event in the code below to execute on click of the textfield.
Please patiently review the code and assist detect where am wrong.

Once this is out of the way, I will use the Validator class you introduced to me, to validate data input in the textfield.

TextComponent loadMe=new TextComponent().text("A20145235")
                             .hint("A20145235")
                             .label("Part No.")
                             .multiline(false);

        loadMe.getField().addActionListener((e) ->{
            execute("www.google.com");
        });

Muchey

unread,
Aug 20, 2018, 8:21:37 AM8/20/18
to CodenameOne Discussions
Am here again.

This code snippet brought me back.
A mouse click on the label executes the actionEvent, however several clicks on the textfield does nothing.

I need the event in the code below to execute on click of the textfield.
Please patiently review the code and assist detect where am wrong.
You might run the lines below and detect on which component the actionEvent executes.

This is seen in the label and not the textfield click event.

Once this is out of the way, I will use the Validator class you introduced to me, to validate data input in the textfield.

TextComponent loadMe=new TextComponent().text("A20145235")
                             .hint("A20145235")
                             .label("Part No.")
                             .multiline(false);

        loadMe.getField().addActionListener((e) ->{
            execute("www.google.com");
        });


On Monday, August 20, 2018 at 1:20:06 PM UTC+1, Muchey wrote:
Am here again.

This code snippet brought me back.
A mouse lick on the label executes the actionEvent, however several clicks on the textfield does nothing.

I need the event in the code below to execute on click of the textfield.
Please patiently review the code and assist detect where am wrong.

Once this is out of the way, I will use the Validator class you introduced to me, to validate data input in the textfield.

TextComponent loadMe=new TextComponent().text("A20145235")
                             .hint("A20145235")
                             .label("Part No.")
                             .multiline(false);

        loadMe.getField().addActionListener((e) ->{
            execute("www.google.com");
        });

Million thanks. 

Shai Almog

unread,
Aug 21, 2018, 12:27:38 AM8/21/18
to CodenameOne Discussions
execute expects a full URL (including https) but notice that the action event can fire more than once in valid cases so you shouldn't do things like opening a URL. For validation it should be OK though. You can use something like regex validator or just implement the constraint interface and add a constraint.

Muchey

unread,
Aug 23, 2018, 2:46:19 AM8/23/18
to CodenameOne Discussions
Thanks Shai.

However, am 'Most Concerned' about the 'Component' where the action event executes.

The action event executes on the LABEL and not the textfield.

If you run the above code snippet, click on the textfield and nothing happens.

The event fires on click of the Label, and not the textfield.

Shai Almog

unread,
Aug 24, 2018, 12:28:15 AM8/24/18
to CodenameOne Discussions
Clicking a text field isn't an action so you won't get an event. Neither is typing into it, but typing and leaving will fire an event from the text field. Notice that the source component of that event is indeed the text field.

Muchey

unread,
Aug 24, 2018, 3:57:37 AM8/24/18
to CodenameOne Discussions
Much clearer Shai, 

Milion thanks.

Would include the validator or constraint interface you shared, as they have clean implementation.
Reply all
Reply to author
Forward
0 new messages