Example:
public class MyWindow extends Window {
private final Panel panel = new Panel(new Layout());
private final TextField textField = new TextField();
private TextArea textArea;
private String value;
public MyWindow(String value) {
this.value = value;
setHeading("Example");
textArea = createTextArea();
panel.add(textArea);
panel.add(textField);
add(panel);
}
private TextArea createTextArea() {
TextArea textArea = new TextArea();
textArea.setValue(value);
textArea.setToolTip("tooltip");
}
}
Keep eye on TextArea - What is bestway to initialize this field? Inline field initialization or default constructor?
Should I do as above or maybe like this:
public class MyWindow extends Window {
...
private TextArea textArea = new TextArea();
...
public MyWindow(String value) {
...
setupTextArea();
...
}
private TextArea setupTextArea() {
textArea.setValue(value);
textArea.setToolTip("tooltip");
}
}
I have many more fields to initializing and I can't always declare the field in one line. Sometimes the creation of a single item in the GUI needs to create two other objects. Therefore, the code becomes difficult to read - some fields are initialized in the constructor, and all rest in the declaration. It doesn't look too good in my opinion.
How do you do it?
EDIT: Maybe I have to create all fields in the constructor and pass all informations to methods as arguments?