codename one container with action listener
Asked Answered
A

2

5

I am developing my first mobile App with Codename One. I am trying to get a container to react to an normal click action event. I have a container (note this is not a swing container, but a codename one container), which contains list elements in a box Y_axis layout that is scrollable. so far so good. these elements are containers themselves, which house labels, an image and a star slider.

now, when the user clicks anywhere in the whole element container, I want to switch to another form to show the details of that entry. however, container does not offer to add an action listener. just implementing the actionlistener interface does not help either. Next problem is, the codename one container does not have a mouselistener either, as mobile apps do not have mouses to click.

so, how can I recognize clicking on a container?

Thanks and best regards

Analog answered 19/2, 2017 at 15:21 Comment(4)
Just added additional comments to explain why this is no duplicate. This is not a normal Java Swing container but the codename one framework. In this case, the container does neither have an action nor a mouse listener.Analog
Then why is your question tagged for Swing if it's not a Swing-related question?Kibitka
You don't provide much information about the code, but from what you said I'd do the following: set the image as a button and attach a ActionListener to it. Set the container as a lead component. (container.setLeadComponent(button). This way, the whole Container will assume the button action.Coordinate
@HovercraftFullOfEels please reopen the question, I have an answer for it and swing tag has been removed.Gleesome
G
12

Create a button and give it your actionListener, then set it as the container's leadComponent and the good thing is you don't have to add it to the container.

Button myBtn = new Button();
myBtn.addActionListener(e -> {
    //go to other form here
});

Container myCont = new Container();
myCont.setLeadComponent(myBtn);
Gleesome answered 19/2, 2017 at 20:20 Comment(8)
(-1) Please read or re-read the comments. This is not a Swing question, and so your answer, a most basic Swing type of code answer, is not relevant to the problem at hand.Kibitka
The code above is strictly codenameone which originated from swing. So this answer is so accurate.Gleesome
And hey, kindly remove the thumb down and let the asker give feedback first.Gleesome
Thanks Diamond for clearing that up and also for your solution, will test this out!Analog
I tested this out and it works like a charm. Thanks a lot!Analog
Cool, glad it helps.Gleesome
This helped me as well, so thanks from me too. However, I have additional point here. My container has 2 buttons, one which should be lead component to react to click anywhere in the container and second button like "close" button which should react to action when clicked on the button only. Is this possible to have one lead component and additional small button on the container?Workhouse
@ArturHefczyc The only way I think this would be possible is by using LayeredLayout. I've provided a sample code here: gist.github.com/diamondobama/941b55f85a828a417fd048623ce78069.Gleesome
W
3

Found an answer to my own question in the comment.

After you set lead component as explained by Diamond you can exclude certain components from lead component event handling by setting setBlockLead(true)

Taking the example above we can extend it with the following:

Button leadBtn = new Button();
leadBtn.addActionListener(e -> {
    //Handle action of the lead button
});

Button anotherBtn = new Button();
anotherBtn.addActionListener(e -> {
    //Handle action of the another button
});

Container myCont = new Container();
myCont.add(leadBtn);
myCont.add(anotherBtn);
myCont.setLeadComponent(leadBtn);
anotherBtn.setBlockedLead(true);
Workhouse answered 16/10, 2020 at 22:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.