Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: help with JButton closing a JFrame :)

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with JButton closing a JFrame :)

    hey,
    I made a JButton on my frame, but i need that button to set the frame its on to Visible(false), how do i do that, because i have tried it doesnt recognize the frame i have made.

    import java.awt.Color;
    import java.awt.Frame;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
    public class minecraft {
    		public void cookbook(){
    			JFrame frame = new JFrame("CookBook Menu");
    			JButton butenter = new JButton("ENTER");
    			frame.setVisible(true);
    			frame.setSize(500, 600);
    			frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    			frame.setResizable(false);
    			frame.setLocationRelativeTo(null);
     
    			JPanel buttonPanel = new JPanel();
    	        buttonPanel.setLayout(null);
    	        buttonPanel.setLocation(195, 400);
    	        buttonPanel.setSize(100, 40);
    	        frame.add(buttonPanel);
    	        buttonPanel.add(butenter);
    	        butenter.setLocation(195, 400);
    	        butenter.setSize(100, 40);
     
    	        butenter.addActionListener(null);
     
     
    		new ActionListener(){
    		public void actionPerformed(ActionEvent e) {
    			frame.setVisible(false); // <---- that doesnt work, it doesnt recognize the frame!
     
    		}
     
    		};}
    		}

    its a minecraft java cookbook

    thanks,
    - Scott


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with JButton closing a JFrame :)

    it doesnt recognize the frame
    Can you explain? If there are error messages, please copy and paste the full text.

    Is the frame variable in scope where you are trying to reference it?
    Move its definition so it is visible where you referring to it.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with JButton closing a JFrame :)

    Quote Originally Posted by Norm View Post
    Can you explain? If there are error messages, please copy and paste the full text.

    Is the frame variable in scope where you are trying to reference it?
    Move its definition so it is visible where you referring to it.

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with JButton closing a JFrame :)

    i need the button i made to close (or to make it visible(false)) when i click it.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with JButton closing a JFrame :)

    If there are error messages, please copy and paste the full text.

  6. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with JButton closing a JFrame :)

    Cannot refer to a non-final variable frame inside an inner class defined in a different method

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with JButton closing a JFrame :)

    Have you tried making frame final?
    final JFrame frame = ...

  8. #8
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with JButton closing a JFrame :)

    yes, that removes the error but doesnt set the frame to visible(false) when i press the button
    Last edited by skerridge; December 21st, 2011 at 09:53 AM.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with JButton closing a JFrame :)

    How is the method with the call to the setVisible method called? Where in your code do you call it?
    You have to cause the method to be called by writing some code.

  10. #10
    Junior Member
    Join Date
    Dec 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with JButton closing a JFrame :)

    sorry, im a kind of newbie at java, can you give me an example? or edit some of my code? that would be much appreciated.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with JButton closing a JFrame :)

    Where did you get the code from?
    Why do you expect the actionPerformed method to be called?
    Have you read about how to use ActionListeners?
    Go to this site and read up:
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

  12. #12
    Junior Member
    Join Date
    Dec 2011
    Location
    Land of Nords! (Norway)
    Posts
    6
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with JButton closing a JFrame :)

    I'm not sure but I think "frame.dispose();" would work.

  13. #13
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help with JButton closing a JFrame :)

    Try to use
    System.exit(0)
    or
    frame.dispose()
    These should work better to close a JFrame

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with JButton closing a JFrame :)

    System.exit(0) does a lot more than just closing a frame.

  15. #15
    Junior Member
    Join Date
    Jan 2012
    Location
    Mumbai
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with JButton closing a JFrame :)

    Inspite of this frame.setVisible(false);

    Try this one
    frame.dispose(this);
    Last edited by RaoPatek; January 15th, 2012 at 04:45 AM.

  16. #16
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: help with JButton closing a JFrame :)

    public void dispose()

    Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.

    The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. The states of the recreated Window and its subcomponents will be identical to the states of these objects at the point where the Window was disposed (not accounting for additional modifications between those actions).

    Note: When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate.
    public void setVisible(boolean b)

    Shows or hides this component depending on the value of parameter b.

    Parameters:
    b - if true, shows this component; otherwise, hides this component
    Also, dispose() is the method of java.awt.Window and setVisible() is of java.awt.Component classes.

Similar Threads

  1. How to allow my program to keep runningwithout closing it?
    By esplanade56 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 18th, 2011, 06:11 AM
  2. Replies: 0
    Last Post: May 10th, 2011, 07:02 AM
  3. problem with closing connection to client socket
    By sunitha in forum Java Networking
    Replies: 1
    Last Post: December 11th, 2010, 04:28 AM
  4. [SOLVED] frame closing programmatically
    By nasi in forum AWT / Java Swing
    Replies: 9
    Last Post: May 10th, 2010, 03:43 AM
  5. how to make a simple JButton on a JFrame window?
    By chronoz13 in forum AWT / Java Swing
    Replies: 8
    Last Post: November 20th, 2009, 10:08 PM

Tags for this Thread