Search…
Grid

Overview

Grid is a control that contains a list organized by columns, where each column can receive an individual size and an alignment.

Source Code

1
import totalcross.sys.Settings;
2
import totalcross.ui.Button;
3
import totalcross.ui.Grid;
4
import totalcross.ui.MainWindow;
5
import totalcross.ui.dialog.MessageBox;
6
import totalcross.util.UnitsConverter;
7
import java.util.ArrayList;
8
9
public class GridSample extends MainWindow {
10
11
private final int H = 225;
12
private ArrayList<User> users = new ArrayList<>();
13
private Grid grid;
14
private Button loadButton;
15
private int GAP = UnitsConverter.toPixels(DP + 8);
16
17
public GridSample(){
18
setUIStyle(Settings.Material);
19
}
20
21
@Override
22
public void initUI() {
23
String[] gridCaptions = { "Name", "Phone", "Email" };
24
int gridWidths[] = { -35, -35, -30 };
25
int gridAligns[] = { LEFT, LEFT, LEFT };
26
27
grid = new Grid(gridCaptions, gridWidths, gridAligns, false);
28
grid.verticalLineStyle = Grid.VERT_LINE;
29
30
loadButton = new Button("Load");
31
32
add(grid, LEFT + GAP, TOP + GAP, FILL - GAP, FILL - GAP * 9);
33
add(loadButton, LEFT + GAP, BOTTOM - GAP, FILL - GAP, PREFERRED);
34
35
loadButton.addPressListener( e -> {
36
37
for (int i = 0; i < 5; i++) {
38
users.add(new User("Joao ","99999999","[email protected]","12345678"));
39
}
40
41
if (users.size() > 0) {
42
String items[][] = new String[users.size()][3];
43
for (int i = 0; i < users.size(); i++) {
44
User user = users.get(i);
45
items[i] = new String[] { user.getName(), user.getPhone(), user.getMail() };
46
}
47
grid.setItems(items);
48
} else {
49
MessageBox mb = new MessageBox("Message", "No registered users.", new String[] { "Close" });
50
mb.popup();
51
}
52
});
53
}
54
55
public class User {
56
57
private String name;
58
private String phone;
59
private String mail;
60
private String password;
61
62
public User() {
63
64
}
65
66
public User(String name, String phone, String mail, String password) {
67
this.name = name;
68
this.phone = phone;
69
this.mail = mail;
70
this.password = password;
71
}
72
73
public String getName() {
74
return name;
75
}
76
public void setName(String name) {
77
this.name = name;
78
}
79
public String getPhone() {
80
return phone;
81
}
82
public void setPhone(String phone) {
83
this.phone = phone;
84
}
85
public String getMail() {
86
return mail;
87
}
88
public void setMail(String mail) {
89
this.mail = mail;
90
}
91
public String getPassword() {
92
return password;
93
}
94
public void setPassword(String password) {
95
this.password = password;
96
}
97
98
}
99
}
100
101
Copied!

Methods

Type
Name
Description
Constructor
Grid(String[] captions, int[] widths, int[] aligns, boolean checkEnabled)
Captions for the columns. Cannot be null;
Widths of the columns. If the total width is less than the grid's width, the last column will fill until the grid width;
Alignment of information on the given column;
checkEnabled is True if you want the multi-selection check column;
Constructor
Grid(String[] captions, boolean checkEnabled)
Captions for the columns; checkEnabled is True if you want the multi-selection check column;
Void
setItems(String[][] items)
Sets the grid items to be displayed.
Void
setDataSource(DataSource ds, int nrItems)
Sets the data source of this grid to be the given one.
Void
add(String[] item)
Add a new line.
Void
add(String[] item, int row)
Add a new line at the given index position of the grid.

References

Last modified 3yr ago