flexcookie flexcookie - 6 months ago 9
Java Question

Java Gui opening two windows

Firstly, I'm new to Java GUIs so please bear with me.
I'me creating a pretty simple GUI from a Model-View-Controller setup, and all it does is display some labels, some textfields and some buttons; nothing fancy. I've got the GUI looking how I want with GridBagLayout, but the trouble I'm having is that whenever I call the GUI view class, I have two windows pop up, rather than one. The first window to open is the GridBagLayout with all my textfields and buttons, and the second is just a small window thing with only the minimise, maximise and close buttons (attached a photo).
Annoying pop-up window

My code is as follows (there are two other classes for model and controller)

public class classView extends JFrame {

private classModel model;

public classView(classModel model){
this.model = model;
JFrame mFrame = new JFrame("This is the view Frame");

mFrame.setBounds(400, 400, 300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addComponentsToPane(mFrame.getContentPane());
mFrame.pack();
mFrame.setVisible(true);
}

private void addComponentsToPane(Container pane){
// This is where I add the buttons and other fun stuff
}


The classView is called like this in another class, along with classController and classModel.

public class classGUIBasics {
public static void main(String[] args) throws Exception {
classModel model = new classModel();
classView view = new classView(model);
new classController(model, view);
view.setVisible(true);
}
}


I know it's probably something simple, but what am I doing wrong? How do I suppress this second window?

Thanks in advance!

Answer

According to your code your classView extends JFrame, so in fact classView is a JFrame, hence your view has all the properties of a JFrame.

So your code could be changed to:

class classView extends JFrame {

    private classModel model;

    public classView ( classModel model ) {
        this.model = model;

        this.setBounds( 400, 400, 300, 300 );
        setDefaultCloseOperation( EXIT_ON_CLOSE );
        addComponentsToPane();
        this.pack();
    }

    private void addComponentsToPane (  ) {
        Container pane = this.getContentPane();
        // This is where I add the buttons and other fun stuff
    }

}

And your classGUIBasics remains the same:

public static void main ( String[] args ) throws Exception {
    classModel model = new classModel();
    classView view = new classView( model );
    new classController( model, view );
    view.setVisible( true );
}

Additionally I would recommend to check the Java naming conventions.

Comments