RTextBox tb1 = new RTextBox();
tb1.setPreferredSize(new Dimension(100, 25));
RTextBox tb2 = new RTextBox();
tb2.setPreferredSize(new Dimension(100, 25));
RLabel lb1 = new RLabel();
Function1<String, Integer> convertToInt = new Function1<String, Integer>() {
@Override
public Integer invoke(String value) {
try {
return Integer.valueOf(value);
} catch (Exception ex) {
return 0;
}
}
};
Var<String> res = tb1.getRText().map(convertToInt).merge(0, 0, tb2.getRText().map(convertToInt), new Function2<Integer, Integer, String>() {
@Override
public String invoke(Integer a, Integer b) {
int sum = a + b;
return a + " + " + b + " = " + sum;
}
});
lb1.setText(res);
addition.add(new JLabel("Addition"));
addition.add(tb1);
addition.add(new JLabel("+"));
addition.add(tb2);