How to capture the action of a click on a title that is inside a container and this container is inside a scrollContainer?

2

How to capture the action of a click on a title that is inside a container and this container is inside a scrollContainer?

public void onEvent(Event e) {
            if(e.type == ControlEvent.PRESSED && e.target == ???????){
                System.out.println(e.toString());
                   MessageBox mp = new MessageBox("Titulo foi clicado");
                   mp.setBackColor(0xFFFFFF);
                   mp.setForeColor(0X000000);
                   mp.popup();
               }
        }
    
asked by anonymous 01.11.2018 / 18:24

2 answers

2

And just declare the variable out of initUI(){} that way it will be seen in the whole code, and then you can use it in whatever method you create. I wanted to see the whole code to help more, but hope this helps!

private ScrollContainer sc;
private Container ct;
private Button tt;

public void initUI(){
    sc = new ScrollContainer(){
        public void initUI(){

            ct = new Container(){
                public void initUI(){
                    setBackColor(0x2196f3);

                    tt = new Button("Titulo");
                    tt.setFont(Font.getFont(true, Font.NORMAL_SIZE + 3).asBold());
                    tt.setBackForeColors(0x757575,0xFFFFFF);
                    tt.setBorder(BORDER_NONE);
                    new MessageBox("Mensagem","Titulo foi clicado").popup();
                    add(tt, LEFT, TOP, FILL,FILL);

                }
            };

            add(ct, LEFT, TOP, FILL, PARENTSIZE+10);
        }
    };

    add(sc,LEFT,TOP,FILL,FILL);

}
public void onEvent(Event e) {
    if(e.type == ControlEvent.PRESSED && e.target == tt){
        System.out.println(e.toString());

        MessageBox mp =  new MessageBox("Mensagem","Titulo foi clicado");
         mp.setBackColor(0xFFFFFF);
         mp.setForeColor(0X000000);
         mp.popup();

    }
}
    
01.11.2018 / 22:13
1

An alternative to @J's response. Eric . With my own development bugs in TotalCross.

Among things I codify, I always look for:

  • Use vanilla components when you do not need to change their behavior
  • use the listeners of events for an event-oriented schedule
  • avoid fields that are not needed

What does this mean? I would not create the anonymous classes for Container and ScrollContainer , would keep the instances as local objects and would add a PressListener to the button:

@Override
public void initUI() {
  ScrollContainer sc = new ScrollContainer();
  add(sc, LEFT, TOP, FILL, FILL);

  Container ct = new Container();
  ct.setBackColor(0x2196f3);
  sc.add(ct, LEFT, TOP, FILL, PARENTSIZE+10);

  Button tt = new Button("Titulo");
  tt.setFont(Font.getFont(true, Font.NORMAL_SIZE + 3).asBold());
  tt.setBackForeColors(0x757575,0xFFFFFF);
  tt.setBorder(BORDER_NONE);

  tt.addPressListener(e -> {
    System.out.println(e.toString());
    MessageBox mp = new MessageBox("Titulo foi clicado");
    mp.setBackColor(0xFFFFFF);
    mp.setForeColor(0X000000);
    mp.popup();
  });

  ct.add(tt, LEFT, TOP, FILL,FILL);
}

Why do I do this? Well, the first one I do not see need to override these methods. The second is because this avoids the onEvent -hell that one has when it has many components on the screen, if one can more easily direct which type of event desired. In TotalCross's documentation it is said that using listeners is inefficient, but, frankly, we are dealing with an action triggered by a user event, so there may be a slowness in the handling of the click that will not be felt by the user.

Finally, I do not create unnecessary fields because of aesthetics =)

    
05.12.2018 / 20:14