Sunday, February 10, 2013

Abstraction ui elements with labels

You always have the issue, that you need next to ui elements a label to name the element.
There are many options to solve the problem. First the easy way:
ListBox box=new ListBox();
Label lbl=new Label("Countrys");

this.add(box);
this.add(lbl);
This code a lot of programmers have to do for hundrets of input fields, comboboxes and so on.I would recomment, to build up for your most needed UI Elements a class, which also contains the label and the data containing element.
public class MisTextAreaWithLabel extends MisVerticalPanel {
 private TextArea area;
 private Label lbl;

 public MisTextAreaWithLabel(String label) {
  this.lbl = new Label(label);
  this.area = new TextArea();

  this.add(area);
  this.add(lbl);
 }

 public void setValue(String newValue) {
  this.area.setValue(newValue);
 }

 public String getValue() {
  return this.area.getValue();
 }
}
This code is easyly useable with:
MisTextAreaWithLabel  area=new MisTextAreaWithLabel("firstname");
this.add(area);
By creating a new Panel where the element and it’s description is on, you can save a lot of code and you will be able to change the behavior of all your UI Elements easyly.

No comments:

Post a Comment