Question

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

Was it helpful?

Solution

Start by taking a look at Initial Threads

The Gui class is a little awkward...but's not far from being workable...

In the initialize it is creating the basics of the UI, in the startUI it is switching over to the Event Dispatching Thread, making a new instance of Gui and showing the main frame...

The part that is weird is the creation of the second instance of Gui in the startUI. Instead, you could do something like...

public Gui() 
{   
    // It's arguable, but I'm paranoid, so I prefer to do anything related
    // to the UI within the EDT...
}

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 
            {
                // Initialize here instead...
                initialize();
                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();} 
        }
    });
}

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...

public static void main(String[] args) 
{
    EventQueue.invokeLater(new Runnable() 
    {
        public void run()
        {
            // Initialise the system look and feel...
            try 
            {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) 
            {
            }
            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);             
        }
    });
}

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
}

OTHER TIPS

A better Approach : Gui Class :

public class Gui {
private JFrame mainWinFrm;

public Gui() {
    initialize();

}

private void initialize() {
    mainWinFrm = new JFrame();
    mainWinFrm.setTitle("Inventory Tool");

    JMenuBar menuBar = new JMenuBar();
    mainWinFrm.getContentPane().add(menuBar, BorderLayout.NORTH);

}

public static void startGui() {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception ex) {
    }

    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();
    }
}

}

And MainClass :

public class MainClass {
public static void main(String[] args) 
{
 Gui.startGui();
}

}

It is better way as it is not creating any extra object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top