I'm learning Java at the moment and I'm currently trying to make a GUI using Swing. I've done some reading and people usually prefer and advice to use composition instead of inheritance of e.g. JFrames.
I'm trying to code the GUI by hand instead of using the GUI builders, since they all generate a lot of code and I want to have a proper understanding of what's happening, and I can't seem to do that with the builders.
Questions: Is this a proper use of 'composition' over inheritance, in this case of using JFrames? I want the code behind the GUI to be as efficient as possible, before I begin on expanding the GUI.
Also: Swing is not thread-safe, so I read that it's good to use invokeLater. Am I using it correctly? Can I use it like that in the main, or should it go under the createGUI method?
package com.company;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Main initiateProgram = new Main();
initiateProgram.createGUI();
}
});
}
public void createGUI() {
JFrame programGUI = new JFrame("GUI Program");
programGUI.setSize(500, 500);
programGUI.setLocationRelativeTo(null);
programGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
programGUI.setVisible(true);
// And here on, an ActionListener for e.g. a button...
}
}