0

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?

user2363971
  • 191
  • 1
  • 4
  • 19
  • 2
    http://stackoverflow.com/questions/1994218/should-i-instantiate-instance-variables-on-declaration-or-in-the-constructor – Madhawa Priyashantha Mar 10 '16 at 09:13
  • You could take more of a factory pattern approach. There's no right or wrong way to do this as some ui's are more complex then others, what you need to do is find approaches which you can comfortable with. One thing you want to keep in the back of your mind is, how hard would it be to extend and modify the ui later? – MadProgrammer Mar 10 '16 at 09:26

3 Answers3

1

Since you need to have a more complex initialisation than just simple one line, I would suggest to initialise everything inside the default constructor, from my viewpoint is clearer in term of readability.

If you want to reuse the initialisation code in another overloaded constructor you can then call the default as first statement in the overloaded one.

aleroot
  • 71,077
  • 30
  • 176
  • 213
1

I always do with creating method initComponents, where I initialize all components:

public class MyWindow extends Window{

private final Panel panel;
private final TextField textField;
private TextArea textArea;
private String value;

public MyWindow(String title){
     this.value = value;
     setHeading("Example");
     initComponents();
}

private void initComponents(){
    panel = new Panel(new Layout());
    textField = new TextField();
    textArea = new TextArea();
    textArea.setValue(value);
    textArea.setToolTip("tooltip");
    // initializing any components and settings of these components
}
}
Victor1125
  • 642
  • 5
  • 16
1

See the following example how it could be done.
Use a initialize method to create the specific components in their own method (or class is also optional if for example panels are created within frames)

By the way don't let your class extend from Window, this is considered as bad practice. Rather create a window object within your class. For example in my example I create a Jframe and not extend from Jframe class.

public class Program {

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() { new MainFrame();}
    });
    }
}



public class MainFrame {      
    private JFrame frame;
    private JTextArea textArea_input;
    private JTextArea textArea_output;
    private JButton button_parse;
    private JButton button_createQuestionnaire;
    private Form form;

    public MainFrame() {
    initialize();
    }

    private void initialize() {
    final Context context = new Context();
    createFrame();      
    createTextFieldInput();
    createButtonParse(context);
    createButtonQuestionnaire(context);
    createTextFieldOutput();
    }

    private void createButtonQuestionnaire(final Context context) {
    button_createQuestionnaire = new JButton("Create Questionnaire");
    button_createQuestionnaire.setEnabled(false);
    button_createQuestionnaire.setSize(TEXTFIELD_WIDTH, TEXTFIELD_HEIGHT);
    frame.getContentPane().add(button_createQuestionnaire);     
    button_createQuestionnaire.addMouseListener(buttonClickCreateQuestionnaire(context));
    }
M. Suurland
  • 725
  • 12
  • 31
  • 1
    I use something like this CompositeUI.init(panel) etc. then I no need extends any component. Anyway good point! – user2363971 Mar 10 '16 at 09:45