Grid is a control that contains a list organized by columns, where each column can receive an individual size and an alignment.
import totalcross.sys.Settings;
import totalcross.ui.Button;
import totalcross.ui.Grid;
import totalcross.ui.MainWindow;
import totalcross.ui.dialog.MessageBox;
import totalcross.util.UnitsConverter;
import java.util.ArrayList;
public class GridSample extends MainWindow {
private final int H = 225;
private ArrayList<User> users = new ArrayList<>();
private Grid grid;
private Button loadButton;
private int GAP = UnitsConverter.toPixels(DP + 8);
public GridSample(){
setUIStyle(Settings.Material);
}
@Override
public void initUI() {
String[] gridCaptions = { "Name", "Phone", "Email" };
int gridWidths[] = { -35, -35, -30 };
int gridAligns[] = { LEFT, LEFT, LEFT };
grid = new Grid(gridCaptions, gridWidths, gridAligns, false);
grid.verticalLineStyle = Grid.VERT_LINE;
loadButton = new Button("Load");
add(grid, LEFT + GAP, TOP + GAP, FILL - GAP, FILL - GAP * 9);
add(loadButton, LEFT + GAP, BOTTOM - GAP, FILL - GAP, PREFERRED);
loadButton.addPressListener( e -> {
for (int i = 0; i < 5; i++) {
users.add(new User("Joao ","99999999","joao@j.com","12345678"));
}
if (users.size() > 0) {
String items[][] = new String[users.size()][3];
for (int i = 0; i < users.size(); i++) {
User user = users.get(i);
items[i] = new String[] { user.getName(), user.getPhone(), user.getMail() };
}
grid.setItems(items);
} else {
MessageBox mb = new MessageBox("Message", "No registered users.", new String[] { "Close" });
mb.popup();
}
});
}
public class User {
private String name;
private String phone;
private String mail;
private String password;
public User() {
}
public User(String name, String phone, String mail, String password) {
this.name = name;
this.phone = phone;
this.mail = mail;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
}