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

Asked

Viewed 65 times

2

How to capture the action of a click on a title that is inside a container and that 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();
               }
        }

2 answers

2


And only declare the variable outside the initUI(){} this way it will be seen in the whole code, and then you can use it in any method you create. I wanted to see the whole code to help more, but I 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();

    }
}

1

An alternative to Reply from @J. Eric. With my own developmental addictions in Totalcross.

Among things I code, I always look:

  • use the components vanilla when you don’t need to change your behavior
  • use the listeners events for an event-oriented programme
  • avoid fields that are not necessary

What does that mean? That I wouldn’t create the anonymous classes for Container and ScrollContainer, would keep the instances as local objects and would add a PressListener on 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 I don’t see the need to overwrite these methods. The second is because it avoids the onEvent-What you get when you have many components on the screen, you can more easily direct what type of event you want. The Totalcross documentation says that using the listeners It’s inefficient, but honestly? , 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 by aesthetics =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.