Tom T
Tom T

Reputation: 59

Why won't my swing GUI launch from the main class

The problem that I'm facing is that when i launch the program from the GUImain class it will work with no problem but when i try and call it up from the main class nothing opens.

The only message i get from the console is:

<terminated> Main [Java Application] /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/bin/java (5 Dec 2014 14:39:29)

This is my main class code:

public class Main 
{
    public static void main(String[] args)
    {
        int i = 0;
        int t = 0;
        int st = 0;
        int h = 0;

        Texts textObject = new Texts();
        textObject.TextList();

        Commands commandObject = new Commands();
        commandObject.commands();

        GUImain guiObject = new GUImain();
    }
}

This is my GUImain class

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import javax.swing.JTextField;

public class GUImain 
{
    private JFrame frame;
    private JTextField textField;


    //Launch the application.
    public static void main(String[] args)
    {
        GUImain window = new GUImain();
        window.frame.setVisible(true);
    }

    //Create the application.
    public GUImain() 
    {
        frame = new JFrame();
        frame.setBounds(100, 100, 611, 471);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JButton btnInventory = new JButton("Inventory");
        btnInventory.setBounds(514, 6, 91, 29);
        frame.getContentPane().add(btnInventory);

        JButton btnLoad = new JButton("Load");
        btnLoad.setBounds(453, 6, 68, 29);
        frame.getContentPane().add(btnLoad);

        JButton btnSave = new JButton("Save");
        btnSave.setBounds(404, 6, 54, 29);
        frame.getContentPane().add(btnSave);

        JButton btnOptions = new JButton("Options");
        btnOptions.setBounds(335, 6, 76, 29);
        frame.getContentPane().add(btnOptions);

        JTextArea History = new JTextArea();
        History.setText("lol");
        History.setBounds(6, 6, 329, 343);
        frame.getContentPane().add(History);

        JButton btnEnter = new JButton("Enter");
        btnEnter.setBounds(518, 404, 85, 39);
        frame.getContentPane().add(btnEnter);

        JScrollBar scrollBar = new JScrollBar();
        scrollBar.setBounds(320, 6, 15, 338);
        frame.getContentPane().add(scrollBar);

        textField = new JTextField();
        textField.setBounds(5, 410, 508, 28);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        JTextArea textArea = new JTextArea();
        textArea.setBounds(6, 357, 600, 42);
        frame.getContentPane().add(textArea);

        JLabel lblMapGoesHere = new JLabel("Map goes here");
        lblMapGoesHere.setBounds(342, 37, 263, 312);
        frame.getContentPane().add(lblMapGoesHere);
    }
}

Upvotes: 0

Views: 1930

Answers (2)

dic19
dic19

Reputation: 17971

The main method in your Main class just creates a new GUImain object which contains a JFrame instance called frame, but it is never made visible:

public class Main {
    public static void main(String[] args) {
        ...
        GUImain guiObject = new GUImain();
    }
}

You can add a public getter to this frame class member in order to make it visible:

public class GUImain {
   ...
   public JFrame getFrame() {
       return this.frame;
   }
}

public class Main {
    public static void main(String[] args) {
        ...
        GUImain guiObject = new GUImain();
        guiObject.getFrame().setVisible(true);
    }
}

Or just add a new method to the GUImain in order to display the frame:

public class GUImain {
   ...
   public void displayGUI() {
       this.frame.setVisible(true);
   }
}

public class Main {
    public static void main(String[] args) {
        ...
        GUImain guiObject = new GUImain();
        guiObject.displayGUI();
    }
}

Side note

Swing components should be created and updated in the context of the Evevent Dispatch Thread (EDT). See Initial Threads.

Upvotes: 2

Isuru Pathirana
Isuru Pathirana

Reputation: 1118

You are just creating a instance of GUImain in you Main class. You should either make it visible or run the main method of GUImain. Try adding the line,

frame.setVisible(true);

to the end of the constructor of GUImain. And you can remove

window.frame.setVisible(true);

from the main method.

This will fix the problem. But this is not a good way of doing this.

Upvotes: 2

Related Questions