I have the following application. It works, I am just trying to understand it better.
So from the main class I make the following call
public static void main(String[] args)
{
Gui gui = new Gui();
gui.startGui();
}
In the GUI Class
public Gui()
{
initialize();
}
private void initialize()
{
mainWinFrm = new JFrame();
mainWinFrm.setTitle("Inventory Tool");
JMenuBar menuBar = new JMenuBar();
mainWinFrm.getContentPane().add(menuBar, BorderLayout.NORTH);
.....//allot more GUI staff getting initialize
}
public void startGui()
{
try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {}
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
Gui window = new Gui();
window.mainWinFrm.setLocationRelativeTo(null);
window.mainWinFrm.setMinimumSize(new Dimension(400, 200));
window.mainWinFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.mainWinFrm.pack();
window.mainWinFrm.setVisible(true);
}
catch (Exception e)
{e.printStackTrace();}
}
});
}
The structure of the above code was build by a GUI builder and I guess I am trying to understand whats happening.
The main point that i dont understand is when we construct the object gui in main class it initializes all the variables and then it starts the thread gui.startGui() that method also creates a new GUI object window and initialize all the variables.
This doesn't seem right to me ... but then I am not sure if i am missing something.
Thanks for the advice/help Alexis
Start by taking a look at Initial Threads
The
Guiclass is a little awkward...but's not far from being workable...In the
initializeit is creating the basics of the UI, in thestartUIit is switching over to the Event Dispatching Thread, making a new instance ofGuiand showing the main frame...The part that is weird is the creation of the second instance of
Guiin thestartUI. Instead, you could do something like...This still doesn't sit right with, because now it's possible to access parts of the class before they've being initalised, instead, I would prefer to doing something more like...