Search…
Aligned Labels

Overview

Aligned Label is a Container used to align all controls to the maximum width of a set of labels.
In Totalcross this component is called AlignedLabelContainer.

Source Code

AlignedLabelsSample.java
1
import totalcross.ui.AlignedLabelsContainer;
2
import totalcross.ui.Button;
3
import totalcross.ui.ComboBox;
4
import totalcross.ui.Edit;
5
import totalcross.ui.Label;
6
import totalcross.ui.ListBox;
7
import totalcross.ui.ScrollContainer;
8
import totalcross.util.UnitsConverter;
9
10
public class extends ScrollContainer {
11
private int gap = UnitsConverter.toPixels(10 + DP);
12
private boolean canInsert = true;
13
private ListBox lb;
14
private Label output;
15
16
@Override
17
public void initUI() {
18
uiAdjustmentsBasedOnFontHeightIsSupported = false;
19
setBackForeColors(0xF7F7F7, 0x000000);
20
setScrollBars(false, true);
21
22
String[] labels = { "Name", "Born date", "Telephone", "Address", "City", "Country" };
23
Edit edits[] = new Edit[5];
24
edits[0].transparentBackground = true;
25
Edit.useNativeNumericPad = true;
26
27
for (int i = 0; i < edits.length; i++) {
28
switch (i) {
29
case 1:
30
edits[i] = new Edit("99/99/9999");
31
edits[i].setMode(Edit.NORMAL, true);
32
edits[i].setValidChars(Edit.numbersSet);
33
edits[i].setKeyboard(Edit.KBD_NUMERIC);
34
break;
35
case 2:
36
edits[i] = new Edit("(99)9999-99999");
37
edits[i].setMode(Edit.NORMAL, true);
38
edits[i].setValidChars(Edit.numbersSet);
39
edits[i].setKeyboard(Edit.KBD_NUMERIC);
40
break;
41
default:
42
edits[i] = new Edit();
43
}
44
}
45
46
Label title = new Label("This is an AlignedLabelsContainer.\nAll the content will be automatically aligned.",
47
CENTER, 0, true);
48
title.autoSplit = true;
49
add(title, LEFT + gap, TOP + gap, FILL - gap, PREFERRED);
50
51
AlignedLabelsContainer alc = new AlignedLabelsContainer();
52
alc.uiAdjustmentsBasedOnFontHeightIsSupported = false;
53
alc.labelAlign = RIGHT;
54
55
alc.setInsets(gap, gap, 0, 0);
56
alc.setLabels(labels, edits[0].getPreferredHeight());
57
add(alc, LEFT, AFTER, FILL, PREFERRED);
58
int i;
59
for (i = 0; i < edits.length - 1; i++) {
60
alc.add(edits[i], LEFT + gap, alc.getLineY(i), FILL - gap, PREFERRED);
61
}
62
63
Button btnInsert = new Button("Insert data", (byte) 0);
64
btnInsert.setBackForeColors(0x4583d4, 0xFFFFFF);
65
alc.add(edits[edits.length - 1], LEFT + gap, alc.getLineY(i), edits[3].getWidth() / 2 - gap / 2, PREFERRED);
66
alc.add(btnInsert, RIGHT - gap, CENTER_OF, SAME, PREFERRED, edits[edits.length - 1]);
67
68
ComboBox cbCountry = new ComboBox(new String[] { "Brazil", "USA" });
69
alc.add(cbCountry, LEFT + gap, alc.getLineY(++i), SAME, PREFERRED, edits[edits.length - 1]);
70
71
Button btnClear = new Button("CLEAR DATA", (byte) 0);
72
alc.add(btnClear, RIGHT - gap, CENTER_OF, SAME, PREFERRED);
73
74
btnInsert.addPressListener(e -> {
75
if (canInsert) {
76
lb = new ListBox();
77
for (int j = 0; j < edits.length; j++)
78
lb.add(labels[j] + ": " + edits[j].getText());
79
if (cbCountry.getSelectedIndex() != -1)
80
lb.add("Country: " + cbCountry.getSelectedItem());
81
else
82
lb.add("Country: ");
83
84
output = new Label("OUTPUT:");
85
output.setFont(font.asBold());
86
add(output, CENTER, AFTER);
87
add(lb, CENTER, AFTER + gap, SCREENSIZE + 80, PREFERRED);
88
canInsert = false;
89
90
scrollToControl(lb);
91
} else {
92
lb.removeAll();
93
for (int j = 0; j < edits.length; j++)
94
lb.add(labels[j] + ": " + edits[j].getText());
95
if (cbCountry.getSelectedIndex() != -1)
96
lb.add("Country: " + cbCountry.getSelectedItem());
97
else
98
lb.add("Country: ");
99
}
100
// reposition(); reposition bugando o edit
101
});
102
103
btnClear.addPressListener(e -> {
104
105
// Cleaning the labels' content
106
for (Edit edit : edits)
107
edit.clear();
108
cbCountry.setSelectedIndex(-1);
109
if (!canInsert) {
110
// Cleaning the output
111
remove(lb);
112
remove(output);
113
canInsert = true;
114
}
115
});
116
}
117
}
Copied!

Attributes

Type
Name
Description
Font
childrenFont
Set this member to the font you want to set to the controls that are added to this container
int[ ]
foreColors
Sets an array with the same number of labels and the colors you want to show for each label
int
labelAlign
The alignment of the labels

Methods

ype
Name
Description
Construtor
AlignedLabelsContainer( )
Creates a new AlignedLabelsContainer without labels
Construtor
AlignedLabelsContainer(String[] labels)
Creates a new AlignedLabelsContainer with the given labels
Construtor
AlignedLabelsContainer(String[] labels, int vgap)
Creates a new AlignedLabelsContainer with the given labels and a vertical gap between the labels
void
add(Control c)
Since this is an AlignedLabelsContainer, use this to add a label
int
getLineY(int line)
Given a line (staring from 0), returns the y position
void
setLabels(String[ ] labels, int vgap)
Sets the labels and the extra gap between rows (which may be 0)

Last modified 3yr ago