Can't dispose of jframe window?












0















I'm trying to dispose of the difficulty window after any one of the difficulty button's are clicked but it won't happen. I've tried .dispose and frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); but i can't get it. Is it just placement or more?



import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;

public class Game extends JFrame{

public static JFrame frame = new JFrame();


private JLabel lab;

public static void main(String args) {

Game difficulty = new Game();
difficulty.setSize(350,105);
difficulty.setTitle("Difficulty.");
difficulty.setVisible(true);
difficulty.setLocationRelativeTo(null);


/**Game sudoku = new Game();
sudoku.setSize(900, 900);
sudoku.setVisible(false);*/

}


public Game(){

setLayout(new FlowLayout());
lab = new JLabel("Please select your difficulty.");
add(lab);

JButton easy;
easy = new JButton("Easy");
add(easy);

easy.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("You clicked the button");
JFrame.dispose();
}
});


JButton medium;
medium = new JButton("Medium");
add(medium);

JButton hard;
hard = new JButton("Hard");
add(hard);

JButton evil;
evil = new JButton("Evil!");
add(evil);

}
}









share|improve this question





























    0















    I'm trying to dispose of the difficulty window after any one of the difficulty button's are clicked but it won't happen. I've tried .dispose and frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); but i can't get it. Is it just placement or more?



    import java.awt.FlowLayout;
    import java.awt.event.*;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.GridLayout;

    public class Game extends JFrame{

    public static JFrame frame = new JFrame();


    private JLabel lab;

    public static void main(String args) {

    Game difficulty = new Game();
    difficulty.setSize(350,105);
    difficulty.setTitle("Difficulty.");
    difficulty.setVisible(true);
    difficulty.setLocationRelativeTo(null);


    /**Game sudoku = new Game();
    sudoku.setSize(900, 900);
    sudoku.setVisible(false);*/

    }


    public Game(){

    setLayout(new FlowLayout());
    lab = new JLabel("Please select your difficulty.");
    add(lab);

    JButton easy;
    easy = new JButton("Easy");
    add(easy);

    easy.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e)
    {
    //Execute when button is pressed
    System.out.println("You clicked the button");
    JFrame.dispose();
    }
    });


    JButton medium;
    medium = new JButton("Medium");
    add(medium);

    JButton hard;
    hard = new JButton("Hard");
    add(hard);

    JButton evil;
    evil = new JButton("Evil!");
    add(evil);

    }
    }









    share|improve this question



























      0












      0








      0








      I'm trying to dispose of the difficulty window after any one of the difficulty button's are clicked but it won't happen. I've tried .dispose and frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); but i can't get it. Is it just placement or more?



      import java.awt.FlowLayout;
      import java.awt.event.*;
      import javax.swing.JButton;
      import javax.swing.JDialog;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JOptionPane;
      import javax.swing.JTextField;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.GridLayout;

      public class Game extends JFrame{

      public static JFrame frame = new JFrame();


      private JLabel lab;

      public static void main(String args) {

      Game difficulty = new Game();
      difficulty.setSize(350,105);
      difficulty.setTitle("Difficulty.");
      difficulty.setVisible(true);
      difficulty.setLocationRelativeTo(null);


      /**Game sudoku = new Game();
      sudoku.setSize(900, 900);
      sudoku.setVisible(false);*/

      }


      public Game(){

      setLayout(new FlowLayout());
      lab = new JLabel("Please select your difficulty.");
      add(lab);

      JButton easy;
      easy = new JButton("Easy");
      add(easy);

      easy.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e)
      {
      //Execute when button is pressed
      System.out.println("You clicked the button");
      JFrame.dispose();
      }
      });


      JButton medium;
      medium = new JButton("Medium");
      add(medium);

      JButton hard;
      hard = new JButton("Hard");
      add(hard);

      JButton evil;
      evil = new JButton("Evil!");
      add(evil);

      }
      }









      share|improve this question
















      I'm trying to dispose of the difficulty window after any one of the difficulty button's are clicked but it won't happen. I've tried .dispose and frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); but i can't get it. Is it just placement or more?



      import java.awt.FlowLayout;
      import java.awt.event.*;
      import javax.swing.JButton;
      import javax.swing.JDialog;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JOptionPane;
      import javax.swing.JTextField;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.GridLayout;

      public class Game extends JFrame{

      public static JFrame frame = new JFrame();


      private JLabel lab;

      public static void main(String args) {

      Game difficulty = new Game();
      difficulty.setSize(350,105);
      difficulty.setTitle("Difficulty.");
      difficulty.setVisible(true);
      difficulty.setLocationRelativeTo(null);


      /**Game sudoku = new Game();
      sudoku.setSize(900, 900);
      sudoku.setVisible(false);*/

      }


      public Game(){

      setLayout(new FlowLayout());
      lab = new JLabel("Please select your difficulty.");
      add(lab);

      JButton easy;
      easy = new JButton("Easy");
      add(easy);

      easy.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e)
      {
      //Execute when button is pressed
      System.out.println("You clicked the button");
      JFrame.dispose();
      }
      });


      JButton medium;
      medium = new JButton("Medium");
      add(medium);

      JButton hard;
      hard = new JButton("Hard");
      add(hard);

      JButton evil;
      evil = new JButton("Evil!");
      add(evil);

      }
      }






      java swing jframe jbutton dispose






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 8 '14 at 19:16









      Bonifacio2

      2,13332338




      2,13332338










      asked Apr 8 '14 at 18:48









      user3512387user3512387

      2415




      2415
























          6 Answers
          6






          active

          oldest

          votes


















          2














          dispose() method is not a static, so it'll not work by calling it directly from JFrame class



          JFrame.dispose();


          try to do :



          dispose();


          Or to dispose the frame object you have created



          frame.dispose();


          Read more about JFrame






          share|improve this answer

































            5














            First of all you're extending JFrame and creating an object of JFrame, if I'm not wrong, this shouldn't be done.



            public class Game extends JFrame{

            public static JFrame frame = new JFrame();


            And as @Salah said, JFrame is not static, so it should be:



            public JFrame frame = new JFrame();


            To solve your problem, you're disposing a new JFrame (yes, you have 3 JFrames in one class, instead of 1, which is what you want), with: JFrame.dispose(); if you already created an object or you're extending JFrame, you can:



            this.dispose(); //For the extended JFrame


            or



            frame.dispose(); //For the object you created





            share|improve this answer

































              0














              Try setting the jFrame to invisible before disposing it:



              public void disposeJFrame(JFrame frame){
              frame.setVisible(false);
              frame.dispose();
              }





              share|improve this answer































                0














                I had the same problem:



                this.dispose();


                solved my problem.






                share|improve this answer































                  -1














                  If you're wanting to close the whole program, you can use System.exit(0);






                  share|improve this answer































                    -1














                    Instead JFrame.dispose();, use frame.dispose() or JFrame.this.dispose();






                    share|improve this answer























                      Your Answer






                      StackExchange.ifUsing("editor", function () {
                      StackExchange.using("externalEditor", function () {
                      StackExchange.using("snippets", function () {
                      StackExchange.snippets.init();
                      });
                      });
                      }, "code-snippets");

                      StackExchange.ready(function() {
                      var channelOptions = {
                      tags: "".split(" "),
                      id: "1"
                      };
                      initTagRenderer("".split(" "), "".split(" "), channelOptions);

                      StackExchange.using("externalEditor", function() {
                      // Have to fire editor after snippets, if snippets enabled
                      if (StackExchange.settings.snippets.snippetsEnabled) {
                      StackExchange.using("snippets", function() {
                      createEditor();
                      });
                      }
                      else {
                      createEditor();
                      }
                      });

                      function createEditor() {
                      StackExchange.prepareEditor({
                      heartbeatType: 'answer',
                      autoActivateHeartbeat: false,
                      convertImagesToLinks: true,
                      noModals: true,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: 10,
                      bindNavPrevention: true,
                      postfix: "",
                      imageUploader: {
                      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                      allowUrls: true
                      },
                      onDemand: true,
                      discardSelector: ".discard-answer"
                      ,immediatelyShowMarkdownHelp:true
                      });


                      }
                      });














                      draft saved

                      draft discarded


















                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22945712%2fcant-dispose-of-jframe-window%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown

























                      6 Answers
                      6






                      active

                      oldest

                      votes








                      6 Answers
                      6






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      2














                      dispose() method is not a static, so it'll not work by calling it directly from JFrame class



                      JFrame.dispose();


                      try to do :



                      dispose();


                      Or to dispose the frame object you have created



                      frame.dispose();


                      Read more about JFrame






                      share|improve this answer






























                        2














                        dispose() method is not a static, so it'll not work by calling it directly from JFrame class



                        JFrame.dispose();


                        try to do :



                        dispose();


                        Or to dispose the frame object you have created



                        frame.dispose();


                        Read more about JFrame






                        share|improve this answer




























                          2












                          2








                          2







                          dispose() method is not a static, so it'll not work by calling it directly from JFrame class



                          JFrame.dispose();


                          try to do :



                          dispose();


                          Or to dispose the frame object you have created



                          frame.dispose();


                          Read more about JFrame






                          share|improve this answer















                          dispose() method is not a static, so it'll not work by calling it directly from JFrame class



                          JFrame.dispose();


                          try to do :



                          dispose();


                          Or to dispose the frame object you have created



                          frame.dispose();


                          Read more about JFrame







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Apr 8 '14 at 18:57

























                          answered Apr 8 '14 at 18:51









                          SalahSalah

                          7,24921534




                          7,24921534

























                              5














                              First of all you're extending JFrame and creating an object of JFrame, if I'm not wrong, this shouldn't be done.



                              public class Game extends JFrame{

                              public static JFrame frame = new JFrame();


                              And as @Salah said, JFrame is not static, so it should be:



                              public JFrame frame = new JFrame();


                              To solve your problem, you're disposing a new JFrame (yes, you have 3 JFrames in one class, instead of 1, which is what you want), with: JFrame.dispose(); if you already created an object or you're extending JFrame, you can:



                              this.dispose(); //For the extended JFrame


                              or



                              frame.dispose(); //For the object you created





                              share|improve this answer






























                                5














                                First of all you're extending JFrame and creating an object of JFrame, if I'm not wrong, this shouldn't be done.



                                public class Game extends JFrame{

                                public static JFrame frame = new JFrame();


                                And as @Salah said, JFrame is not static, so it should be:



                                public JFrame frame = new JFrame();


                                To solve your problem, you're disposing a new JFrame (yes, you have 3 JFrames in one class, instead of 1, which is what you want), with: JFrame.dispose(); if you already created an object or you're extending JFrame, you can:



                                this.dispose(); //For the extended JFrame


                                or



                                frame.dispose(); //For the object you created





                                share|improve this answer




























                                  5












                                  5








                                  5







                                  First of all you're extending JFrame and creating an object of JFrame, if I'm not wrong, this shouldn't be done.



                                  public class Game extends JFrame{

                                  public static JFrame frame = new JFrame();


                                  And as @Salah said, JFrame is not static, so it should be:



                                  public JFrame frame = new JFrame();


                                  To solve your problem, you're disposing a new JFrame (yes, you have 3 JFrames in one class, instead of 1, which is what you want), with: JFrame.dispose(); if you already created an object or you're extending JFrame, you can:



                                  this.dispose(); //For the extended JFrame


                                  or



                                  frame.dispose(); //For the object you created





                                  share|improve this answer















                                  First of all you're extending JFrame and creating an object of JFrame, if I'm not wrong, this shouldn't be done.



                                  public class Game extends JFrame{

                                  public static JFrame frame = new JFrame();


                                  And as @Salah said, JFrame is not static, so it should be:



                                  public JFrame frame = new JFrame();


                                  To solve your problem, you're disposing a new JFrame (yes, you have 3 JFrames in one class, instead of 1, which is what you want), with: JFrame.dispose(); if you already created an object or you're extending JFrame, you can:



                                  this.dispose(); //For the extended JFrame


                                  or



                                  frame.dispose(); //For the object you created






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited May 23 '17 at 12:10









                                  Community

                                  11




                                  11










                                  answered Apr 8 '14 at 18:58









                                  FrakcoolFrakcool

                                  7,72252557




                                  7,72252557























                                      0














                                      Try setting the jFrame to invisible before disposing it:



                                      public void disposeJFrame(JFrame frame){
                                      frame.setVisible(false);
                                      frame.dispose();
                                      }





                                      share|improve this answer




























                                        0














                                        Try setting the jFrame to invisible before disposing it:



                                        public void disposeJFrame(JFrame frame){
                                        frame.setVisible(false);
                                        frame.dispose();
                                        }





                                        share|improve this answer


























                                          0












                                          0








                                          0







                                          Try setting the jFrame to invisible before disposing it:



                                          public void disposeJFrame(JFrame frame){
                                          frame.setVisible(false);
                                          frame.dispose();
                                          }





                                          share|improve this answer













                                          Try setting the jFrame to invisible before disposing it:



                                          public void disposeJFrame(JFrame frame){
                                          frame.setVisible(false);
                                          frame.dispose();
                                          }






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Apr 8 '14 at 18:54









                                          Roach TenantRoach Tenant

                                          1




                                          1























                                              0














                                              I had the same problem:



                                              this.dispose();


                                              solved my problem.






                                              share|improve this answer




























                                                0














                                                I had the same problem:



                                                this.dispose();


                                                solved my problem.






                                                share|improve this answer


























                                                  0












                                                  0








                                                  0







                                                  I had the same problem:



                                                  this.dispose();


                                                  solved my problem.






                                                  share|improve this answer













                                                  I had the same problem:



                                                  this.dispose();


                                                  solved my problem.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Sep 30 '18 at 8:05









                                                  besartmbesartm

                                                  8116




                                                  8116























                                                      -1














                                                      If you're wanting to close the whole program, you can use System.exit(0);






                                                      share|improve this answer




























                                                        -1














                                                        If you're wanting to close the whole program, you can use System.exit(0);






                                                        share|improve this answer


























                                                          -1












                                                          -1








                                                          -1







                                                          If you're wanting to close the whole program, you can use System.exit(0);






                                                          share|improve this answer













                                                          If you're wanting to close the whole program, you can use System.exit(0);







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Apr 8 '14 at 18:52









                                                          waco001waco001

                                                          391110




                                                          391110























                                                              -1














                                                              Instead JFrame.dispose();, use frame.dispose() or JFrame.this.dispose();






                                                              share|improve this answer




























                                                                -1














                                                                Instead JFrame.dispose();, use frame.dispose() or JFrame.this.dispose();






                                                                share|improve this answer


























                                                                  -1












                                                                  -1








                                                                  -1







                                                                  Instead JFrame.dispose();, use frame.dispose() or JFrame.this.dispose();






                                                                  share|improve this answer













                                                                  Instead JFrame.dispose();, use frame.dispose() or JFrame.this.dispose();







                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered Apr 8 '14 at 18:56









                                                                  eliaselias

                                                                  9,16033056




                                                                  9,16033056






























                                                                      draft saved

                                                                      draft discarded




















































                                                                      Thanks for contributing an answer to Stack Overflow!


                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                      But avoid



                                                                      • Asking for help, clarification, or responding to other answers.

                                                                      • Making statements based on opinion; back them up with references or personal experience.


                                                                      To learn more, see our tips on writing great answers.




                                                                      draft saved


                                                                      draft discarded














                                                                      StackExchange.ready(
                                                                      function () {
                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22945712%2fcant-dispose-of-jframe-window%23new-answer', 'question_page');
                                                                      }
                                                                      );

                                                                      Post as a guest















                                                                      Required, but never shown





















































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown

































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown







                                                                      Popular posts from this blog

                                                                      To store a contact into the json file from server.js file using a class in NodeJS

                                                                      Redirect URL with Chrome Remote Debugging Android Devices

                                                                      Dieringhausen