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