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