1
votes

I'm stuck. I would like to find out which component a user was pointing at when an ActionEvent is passed to a LeadComponent.

In my case I want to know if a user pressed on any point in a Container or at a specific Button which is contained by that Container. But the LeadComponent and that Button are not the same, and shouldn't be. Some text have to get copied to clipboard if the user taps on the green box, and if pressed on the map button another functionality should be triggered instead (see image below). Unfortunately neither ActionEvent.getActualComponent() nor ActionEvent.getSource() returns that Button when it comes to the events received by the LeadComponent, and the ActionListener, I added to the map Button, doesn't receive any events at all. Is there any solution?

green box with map button

After I got to know that subButton.setBlockLead(true) is the right call to do here, I still had the problem that the sub button doesn't receive any action events. As I checked test cases, I found out that the issue is linked to an underlaying Component (in my case it's a MapComponent but it also could be a plain Container). The sub Button does not receive any events in that case:

Subbutton does not receive any events

this does not work for example (follow does not receive any events):

    Container current = f.getContentPane();

    // box with leading behavior
    Button lead = new Button("Leading");
    Button follow = new Button("Following");
    Label justData = new Label("Just Data");
    Container cnt = FlowLayout.encloseIn(lead, justData, follow);
    cnt.setUIID("ToastBar"); //to emphasize the lead area
    cnt.setLeadComponent(lead);
    follow.setBlockLead(true);

    lead.addActionListener(e -> Log.p("Lead pressed"));
    follow.addActionListener(e -> Log.p("Follow pressed"));

    //child for LayeredLayout (positioning the box)
    Container child = BorderLayout.centerAbsolute(cnt);

    //LayeredLayout with dummy container and the specific child on top
    cnt = LayeredLayout.encloseIn(new Container(), child);

    current.add(cnt);

layered layout issue

1

1 Answers

0
votes

You can exclude a specific component from the lead component hierarchy by using unleading:

delete.setBlockLead(true);

This works for me and only sends the right events:

Button lead = new Button("Leading");
Button follow = new Button("Following");
Label justData = new Label("Just Data");
Container cnt = FlowLayout.encloseIn(lead, justData, follow);
cnt.setLeadComponent(lead);
follow.setBlockLead(true);

lead.addActionListener(e -> Log.p("Lead pressed"));
follow.addActionListener(e -> Log.p("Follow pressed"));
current.add(cnt);