0

In my program when a button is clicked the JFrame I'm using should close. However the dispose() method doesn't seems to work. Could anyone help me?

public class SetUp extends JFrame implements ActionListener, Checker {
    JFrame frame= new JFrame("Set Up");

    public mmSetUp() {      
        setSize(500, 300);

        setLayout(new BorderLayout());

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);        

        setVisible(true);
        pack();

    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == Submit) {
            windowclose();          
        }
    }

    public void windowclose() {

        frame.dispose();
    }
}
ale
  • 6,369
  • 7
  • 55
  • 65

4 Answers4

2

Well after checking your code it's probably because of your class extends JFrame and you're adding all your elements to this extended frame.

But you also create a JFrame, this isn't a good practice, 1st of all see The use of multiple JFrames, Good / Bad Practice about using multiple frames.

Now going back into your code, let's see about it, as I said before, you're extending a JFrame on your class like this (I'll refer to this frame as frame1)

public class SetUp extends JFrame implements ActionListener, Checker {

But you also create a JFrame here (I'll refer to this frame frame2):

JFrame frame= new JFrame("Set Up");

You are trying to dispose the frame2, it's actually happening that, but if you want to dispose frame1 (which is the one which has all your elements, i.e. the one you're working with), then on this method, you should change:

public void windowclose() {
    frame.dispose();
}

for this one:

public void windowclose() {

    this.dispose();
}

Also I don't recommend you to extend from JFrame class it's better to create objects from it like you did.

But if you do so, then you should change your code as follows:

public mmSetUp() {  

    frame.setSize(500, 300);

    frame.setLayout(new BorderLayout());

    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);        

    frame.setVisible(true);
    frame.pack();
}

public void actionPerformed(ActionEvent e) {

    if (e.getSource() == Submit) {
        windowclose();          
    }
}

public void windowclose() {

    frame.dispose();
}

I'm not 100% sure this works or compiles, since I'm not at my PC atm, so I can't create a full example, but at 1st view that's what is happening on your class. In 3 more hours or so, I can post a compilable example.

But please check it

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
2

You have 2 JFrames. One, that is being extended. Two, the one called frame.

First of all, you have only made one mistake.

Change windowclose() to:

public void windowclose() {
    dispose(); // Removed frame part
}

What you have done is, completely set up the extended JFrame from the start and left frame alone so its idle.

When windowclose() is called, it disposes the idle frame which essentially is useless as you have not even modified anything except for the title.

After you have changed windowclose() the code should work, now you can get rid of

JFrame frame= new JFrame("Set Up");

But if you want to keep everything neater, do as Frakcool said and remove extends JFrame

Sk4llsRPG
  • 259
  • 4
  • 13
1

I think you want to call frame.setVisible( false ) instead.

Edit: your program has several logic errors. Try this version instead:

  public class WindowCloseTest
  {
     public static void main( String[] args )
     {
        new SetUp().mmSetUp();
     }
  }
  class SetUp implements ActionListener {
      JFrame frame= new JFrame("Set Up");

      public void mmSetUp() {      
          frame.setSize(500, 300);

          JButton b = new JButton( "Submit");
          b.addActionListener(this);
          frame.add( b, BorderLayout.SOUTH );

          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.setLocationRelativeTo(null);        
          frame.pack();
          frame.setVisible(true);
      }

      public void actionPerformed(ActionEvent e) {
         System.out.println( e );
          if (e.getActionCommand() == "Submit") {
              windowclose();          
          }
      }

      public void windowclose() {

          frame.dispose();
      }

  }
markspace
  • 10,621
  • 3
  • 25
  • 39
0

I agree with @user2338547, there are some code you didn't show us that might cause the problem, I code my version of the use for dispose method, hope it can give you some idea of how to use the method

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;

    public class GuiTest extends JFrame{

        static GuiTest frame=new GuiTest("test");
        private JPanel panel=new JPanel();
        private JButton button=new JButton("close");
        private Button buttonListener=new Button();

        public GuiTest(String title){
            super(title);
            add(panel);
            panel.add(button);
            button.addActionListener(buttonListener);
        }
        public static void main(String[] args){
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            frame.setSize(100,100);
            frame.setVisible(true);
        }
        class Button implements ActionListener{
            public void actionPerformed(ActionEvent e){
            frame.dispose();
            }
        }
    }