Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By camickr

Thread: How to close a JFrame when opening a new one?

  1. #1
    Larss is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default How to close a JFrame when opening a new one?

    Hey guys i can't figure out how to close a JFrame when opening a new JFrame. I tryed System.close but it will close the process thats not what im looking for.

    this is my code:

    Java Code:
    package magicboxinterface;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    /**
     *
     * @author Lars
     */
    
    public class MagicBoxInterface extends JPanel   {
       
        
        public MagicBoxInterface() {
            setOpaque(false);
            //setLayout(new FlowLayout());
            setLayout(null);
        }
    
        public static void main(String[] args) {
            
            ImageIcon geluidenknop = new ImageIcon("C:\\Users\\Lars\\Documents\\NetBeansProjects\\MagicBoxInterface\\src\\magicboxinterface\\resources\\geluiden.png");
            ImageIcon memoryknop = new ImageIcon("C:\\Users\\Lars\\Documents\\NetBeansProjects\\MagicBoxInterface\\src\\magicboxinterface\\resources\\memory.png");
            
            JButton button = new JButton(geluidenknop);
            JButton button2 = new JButton(memoryknop);
            
            button.setBounds(270,250,451,87);
            button2.setBounds(270,400,451,87);
            
            
            JFrame myFrame = new JFrame("Magic Box");
            
            MagicBoxInterface frame = new MagicBoxInterface();
           
            frame.add(button);
            frame.add(button2);
            
            myFrame.add(frame);
            myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            myFrame.setSize(1024,768);
            myFrame.setVisible(true);
            myFrame.setLocationRelativeTo(null);
            
                
            button.addActionListener(new Action());
            button2.addActionListener(new Action1());
            
        
        }
            
            static class Action implements ActionListener {
                
                @Override
                public void actionPerformed (ActionEvent e){
                    Menu2 menu2  = new Menu2();                          
                }  
        }   
         
            
           static class Action1 implements ActionListener {
                
                @Override
                public void actionPerformed (ActionEvent e){
                    
                    Menu4 menu4  = new Menu4();                          
                }  
        }       
            
        @Override
        public void paint(Graphics g) {
            
            Image a = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Lars\\Documents\\NetBeansProjects\\MagicBoxInterface\\src\\magicboxinterface\\resources\\menu.jpg");
            g.drawImage(a,0,0,getSize().width,getSize().height,this);
            super.paint(g);
        }   
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: How to close a JFrame when opening a new one?

    Don't override the paint() method of a JFrame. Custom painting is done by overriding the paintComponent() method of a JPanel. Then you can add the panel to the frame.

    Don't use a null layout and setBounds(). Swing was designed to be used with layout managers.

    Why are you extending JPanel for your MagicBoxInterface class? You didn't change the behaviour of the panel in any way. If you do extend JPanel, the the code to add the buttons to the panel should be defined in the class as well. This code doesn't belong in your main() method.

    To close the frame you can use frame.dispose(). You may also want to change the default close operation.
    sevoxx likes this.

  3. #3
    Larss is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default Re: How to close a JFrame when opening a new one?

    Hey thanks for your answer im having a hard time doing this. What i am doing right now is using JFrames for each of my sub menu's. I know i should use panels but i might be trying that later.
    What i really want to happen right now is to close the JFrames when i open a new menu. I do know about framename.dispose() but i cant figure where to put that since everything i put in my actionslistener method doesn't listen to my code thats above it. I hope u can help me out with this.

  4. #4
    Larss is offline Member
    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Default Re: How to close a JFrame when opening a new one?

    bump

  5. #5
    sevoxx is offline Oracle Java Developer
    Join Date
    Apr 2013
    Location
    In a PineApple Under the Sea
    Posts
    20
    Rep Power
    0

    Default Re: How to close a JFrame when opening a new one?

    Quote Originally Posted by Larss
    What i am doing right now is using JFrames for each of my sub menu's. I know i should use panels but i might be trying that later.
    When camickr says "Don't", I feel the need to emphasize on that. First fix the smaller problems in your code, the way you speak of it, it seems you think that this is the correct way of doing things -- it's not, your code is bad. Fix your code and things will become much more clear and you may even find yourself understanding why you're in the situation you are. Why are you extending JPanel? Why are you running all your code in your main method? Why are you overriding the paint() method? Extend JFrame at the least. Create a new instance in your main method and run the code from another method or at least within the constructor. What you're doing isn't only just a *shouldn't* case, it's wrong.

    You're creating a MagicBoxInterface instance in the middle of your main method and boxed it as 'frame', which is not incorrect, but only confusing to everyone reading your code and more than likely to you too. It's not a frame, because you've extended JPanel for who-knows what reason.

    Java Code:
    MagicBoxInterface frame = new MagicBoxInterface();

    Quote Originally Posted by Larss
    What i really want to happen right now is to close the JFrames when i open a new menu. I do know about framename.dispose() but i cant figure where to put that since everything i put in my actionslistener method doesn't listen to my code thats above it. I hope u can help me out with this.
    Quote Originally Posted by Larss
    bump
    So... What is your question? You originally asked how to dispose of a JFrame -- that has been answered. Now you want to know where to put that line of code? Put it after the newly created JFrame instance as that's what your original question implies doesn't it?
    Last edited by sevoxx; 05-16-2013 at 12:56 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How to close a JFrame when opening a new one?

    Quote Originally Posted by sevoxx View Post
    Why are you extending JPanel? Why are you running all your code in your main method? Why are you overriding the paint() method? Extend JFrame at the least.
    Um, no.
    There is almost never a reason to extend JFrame.
    Extending JPanel is the correct thing to do if you want to draw stuff (ie override paintComponent).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    sevoxx is offline Oracle Java Developer
    Join Date
    Apr 2013
    Location
    In a PineApple Under the Sea
    Posts
    20
    Rep Power
    0

    Default Re: How to close a JFrame when opening a new one?

    Quote Originally Posted by Tolls View Post
    Um, no.
    There is almost never a reason to extend JFrame.
    Extending JPanel is the correct thing to do if you want to draw stuff (ie override paintComponent).
    So let me get this straight. You're telling me that the Java standard in almost every IDE is incorrect? We should notify Oracle to update their documentation.
    Java Code:
    public class MyFrame extends JFrame
    {
           private MyFrame()
           {
                 
           }
    }
    And I should be creating a JFrame instance within my class instead of extending it?

    Java Code:
    public class MyFrame
    {
          private MyFrame()
          {
               JFrame myFrame = new JFrame();
          }
    }
    Edit:
    -----

    Quote Originally Posted by Tolls
    Extending JPanel is the correct thing to do if you want to draw stuff (ie override paintComponent).
    As far as I can tell, that's not what's happening here other than overriding the paint method to draw an image to a JPanel which is to be placed added to the JFrame. That's all dandy, but that's a tiny crumb of the code to the core construction code within the main method which has very little to do with drawing stuff.
    Last edited by sevoxx; 05-16-2013 at 01:36 PM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How to close a JFrame when opening a new one?

    Yep.
    Extending JFrame just to populate it with stuff is like extending ArrayList simply to fill it with objects.
    Unless you are changing the actual functionality of the class (which, as I said, is almost never the case with a JFrame) then don't extend it.

    The very first example in the Oracle Swing tutorials is this one:
    http://docs.oracle.com/javase/tutori...LevelDemo.java

    No extending the frame there.
    And (though I haven't look at every demo in the tutorial) that is the case in all the examples I've seen.

    ETA: And I know it's not the root cause of the problem, I was merely correcting your assertion that the OP should be extending JFrame.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: How to close a JFrame when opening a new one?

    Yes, your second approach is the preferred (but you may want to make myFrame an instance variable). That is standard practice.

    So let me get this straight. You're telling me that the Java standard in almost every IDE is incorrect? We should notify Oracle to update their documentation.
    Can you please cite some examples where it is recommended to extend JFrame? There may be some very rare exceptions but normally it is not necessary.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    sevoxx is offline Oracle Java Developer
    Join Date
    Apr 2013
    Location
    In a PineApple Under the Sea
    Posts
    20
    Rep Power
    0

    Default Re: How to close a JFrame when opening a new one?

    Quote Originally Posted by jim
    Um, no. There is almost never a reason to extend JFrame.
    Why not? Extending is loaded into the memory stack and creating an instance is loaded into the memory heap, why do you take the heap as preference?

    Quote Originally Posted by jim
    Unless you are changing the actual functionality of the class then don't extend it.
    Why not? Extending is loaded into the memory stack and creating an instance is loaded into the memory heap, "

    Quote Originally Posted by jim829
    Can you please cite some examples where it is recommended to extend JFrame? There may be some very rare exceptions but normally it is not necessary.
    I'm not aware of any recommended examples within this thread, infact I'm not aware of any recommendations at all, other than the opposing statement by Tolls. My reply on extending was a suggestion, hence; "at the least", but I'm sure you may find some arguable examples in the Oracle documentation page (see: docs.oracle.com) - Maybe you could provide me with some examples where one should not extend a JFrame, using the original subject of this thread would be fantastic.

    Quote Originally Posted by sevoxx
    Extend JFrame at the least.
    The only thing being called in the class created extending JPanel is the paint() override. Maybe I should have emphasized on the 'at least' rather than the 'dont', but is it entirely necessary to extend to a JPanel to override the paint method which should never be done anyway? Seems a little overkill, there are ways of executing the users desires without even using a JPanel (That could be argued right back at you) -- Yes fields could be instantated instead, but due to the idea that the user is not too experienced I stated another approach which, assuming the user is using an IDE, is already written as a default template in most popular IDE's when creating that particular class type.

    The point of my thread reply was to assist a [beginner [from what I can tell]] Java user. I'm not giving my professional answer, merely a solution to cleaner, working code. Should you want to have a constructive argument, I am completely willing, but I will not hijack a another users topic.
    Last edited by sevoxx; 05-16-2013 at 04:49 PM. Reason: Removed potentially upsetting comment.

  11. #11
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: How to close a JFrame when opening a new one?

    First, you misquoted the first two quotes. Second, no one hijacked anything in this thread. Telling someone not to override paint is proper if someone sees it is wrong (even if they didn't ask). It's called being helpful. Extending JPanel and overriding paintComponent then adding to JFrame is necessary to let the EDT repaint the components. Adding JPanel to JFrame is the preferred technique. Check it out for yourself by visiting the Java Tutorials in my signature.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: How to close a JFrame when opening a new one?

    And I should be creating a JFrame instance within my class instead of extending it?
    Yes.

    Search the web using "composition vs inheritance". The general opinion is to use composition unless you are actually changing the behaviour of a component.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How to close a JFrame when opening a new one?

    Quote Originally Posted by sevoxx View Post
    Why not? Extending is loaded into the memory stack and creating an instance is loaded into the memory heap, why do you take the heap as preference?
    What on earth is that even whittering on about?
    In both cases you instantiate a class, hence on the heap.
    In one case it's a class that (erroneously) extends JFrame, in the other it is a vanilla JFrame.

    The whole point is that structurally the OP has it correct bar the use of paint() instead of paintComponent().
    As camickr says, you override paintComponent() on a JPanel to do custom painting.
    The OP is (presumably) doing custom painting.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    sevoxx is offline Oracle Java Developer
    Join Date
    Apr 2013
    Location
    In a PineApple Under the Sea
    Posts
    20
    Rep Power
    0

    Default Re: How to close a JFrame when opening a new one?

    Quote Originally Posted by jim829 View Post
    First, you misquoted the first two quotes.
    Firstly, my bad on the quotes.

    Quote Originally Posted by jim829 View Post
    Second, no one hijacked anything in this thread. Telling someone not to override paint is proper if someone sees it is wrong (even if they didn't ask). It's called being helpful.
    Second, I mentioned that overriding the paint method is not correct, so a thank you is in order I guess?

    Quote Originally Posted by jim829 View Post
    Extending JPanel and overriding paintComponent then adding to JFrame is necessary to let the EDT repaint the components.
    No.

    Quote Originally Posted by jim829 View Post
    Adding JPanel to JFrame is the preferred technique. Check it out for yourself by visiting the Java Tutorials in my signature.
    Yes. I agree, preferred, not required. No-one argued this.

  15. #15
    sevoxx is offline Oracle Java Developer
    Join Date
    Apr 2013
    Location
    In a PineApple Under the Sea
    Posts
    20
    Rep Power
    0

    Default Re: How to close a JFrame when opening a new one?

    Quote Originally Posted by Tolls View Post
    What on earth is that even whittering on about?
    In both cases you instantiate a class, hence on the heap.
    In one case it's a class that (erroneously) extends JFrame, in the other it is a vanilla JFrame.

    The whole point is that structurally the OP has it correct bar the use of paint() instead of paintComponent().
    As camickr says, you override paintComponent() on a JPanel to do custom painting.
    The OP is (presumably) doing custom painting.
    Still doesn't answer my question, your first line is sent write back at you. I bring up the stack and heap because in my experience that's the only issue that comes up when extending or not extending to JFrame. I'd still like an answer to your original statement. Why should I never extend a JFrame?

    Quote Originally Posted by Tolls View Post
    In both cases you instantiate a class, hence on the heap.
    That's a negative. JVM

  16. #16
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: How to close a JFrame when opening a new one?

    Quote Originally Posted by sevoxx View Post
    No.
    OK. Since you didn't fully explain your answer, please demonstrate how to get a JPanel to invoke its paintComponent method without adding it to any other component. I mentioned adding it to JFrame because that was part of the discussion. I really don't know how to do this (other than calling getGraphics() and then invoking paintComponent directly-- which would be atrocious).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How to close a JFrame when opening a new one?

    Quote Originally Posted by sevoxx View Post
    Still doesn't answer my question, your first line is sent write back at you. I bring up the stack and heap because in my experience that's the only issue that comes up when extending or not extending to JFrame. I'd still like an answer to your original statement. Why should I never extend a JFrame?
    I never said "never".
    I said "almost never".
    OO programming 101, extending a class when you are not extending the functionality of that class is poor practice. In the case of a JFrame, unless you are altering or extending how it works as a JFrame then extending it is wrong.
    As I said earlier, would you extend an ArrayList to simply populate it with stuff?

    Quote Originally Posted by sevoxx View Post
    That's a negative. JVM
    You clearly don't understand what's written there.
    If in my initial invokeLater() I create a JFrame and populate it with a JPanel with a couple of JButtons, then I have stuck 4 things (plus whatever those classes hold) on the heap. If I instead create a MYJFrame inside of which I have written code to create a JPanel with a couple of JButtons, then I have still created 4 things on the heap.

    So, how does extending JFrame reduce the heap and increase the stack? I've still stuck 4 objects on there.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. donīt close a jFrame
    By flaquitqm in forum AWT / Swing
    Replies: 4
    Last Post: 04-25-2010, 03:57 AM
  2. Can't close JFrame
    By dunafrothint in forum AWT / Swing
    Replies: 5
    Last Post: 12-16-2009, 06:00 PM
  3. opening of an exe in JFrame
    By smartsubroto in forum New To Java
    Replies: 3
    Last Post: 07-16-2008, 06:01 AM
  4. JFrame Conditional Close
    By hemanthjava in forum AWT / Swing
    Replies: 10
    Last Post: 06-29-2008, 08:49 AM
  5. How to close a JFrame
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •