3
votes

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

2
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. - Lequi
Then why is your question tagged for Swing if it's not a Swing-related question? - Hovercraft Full Of Eels
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. - Carlos Verdier
@HovercraftFullOfEels please reopen the question, I have an answer for it and swing tag has been removed. - Diamond

2 Answers

11
votes

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);
0
votes

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);