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.

Page 1 of 3 123 LastLast
Results 1 to 25 of 64

Thread: dispose(); not working

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default dispose(); not working

    Hi,

    Aim is to close previou
    Last edited by newtolearningjava; April 26th, 2014 at 05:43 PM.


  2. #2
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: dispose(); not working

    Try this.dispose(); in the frame you want to close
    example:
    public void actionPerformed(ActionEvent e)
    {

    this.dispose();

    }

    I don't know how you are opening the new frame right now, but it might be a good idea to make a button to open the new one and close the old one.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: dispose(); not working

    You need a reference to the object to be able to call its methods.

    The this keyword refers to the object that it is executing in. The example shown:
    this.dispose()
    doesn't do anything because this is the default for a call to a method without an object reference.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: dispose(); not working

    Quote Originally Posted by Norm View Post
    You need a reference to the object to be able to call its methods.
    ou need a reference to the object to be able to call its methods.
    Last edited by newtolearningjava; April 26th, 2014 at 05:45 PM.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: dispose(); not working

    I thought you were asking about closing one other frame. Why the array? And what do you mean by "smoothly?"

  6. #6
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: dispose(); not working

    YES Greg i have opened
    Last edited by newtolearningjava; April 26th, 2014 at 05:45 PM.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: dispose(); not working

    A 'reference to an object' can be passed through a constructor or obtained from a getter method. In the scenario you've described, it seems the more typical approach would be to use the second class' constructor. Something like this:
    // the first frame in a sequence of displayed frames
    class Class1 extends JFrame
    {
        // default Class1() constructor
        public Class1()
        {
            // everything needed to create a Class1 object and
            // display it, get what's needed from it, etc.
     
            // after Class1 is done, transfer control to Class2
            transferControlToClass2();
        }
     
        // a method that passes control from Class1 to Class2
        private void transferControlToClass2()
        {
            new Class2( this );
        }
     
    } // end class Class1
     
    //the second frame in a sequence of displayed frames
    class Class2 extends JFrame
    {
        // a reference to a Class1 object
        // (not used but included to show you how it could be) 
        Class1 class1;
     
        // default Class2() constructor
        public Class2( Class1 class1 )
        {
            // removes Class1 from the screen using the reference
            // passed to the constructor
            class1.dispose();
     
            // everything needed to create a Class2 object and
            // display it, get what's needed from it, etc.
        }
     
        // and so on . . .
     
    } // end class Class2
    BTW - I wouldn't use multiple JFrames as cascading user interfaces. I prefer to use JDialogs as the child frames, but your application might be slightly different than I imagine.

  8. #8
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: dispose(); not working

    cannot find symbol symbol:
    Last edited by newtolearningjava; April 26th, 2014 at 05:46 PM.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: dispose(); not working

    I assume you mean this error: cannot find symbol symbol: method dispose()

    If the object on which the dispose() method is being called is a JFrame, then IT'S IMPOSSIBLE to get that error. You're not doing something right, clearly not following the example I posted. Post your code if you want real help.

  10. #10
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: dispose(); not working

    i cant
    Last edited by newtolearningjava; April 26th, 2014 at 05:46 PM.

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: dispose(); not working

    Don't make it difficult. Post the code that throws the error; the code that calls the dispose() method, preferably that whole class.

  12. #12
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: dispose(); not working

    public class game extends JFrame{
    //some code
     
     
    }}
    Last edited by newtolearningjava; April 26th, 2014 at 05:46 PM.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: dispose(); not working

    Where is the class with the dispose() method? You need a reference to the class's object to be able to call its dispose() method. The posted code does not show a class that has a dispose() method.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: dispose(); not working

    i have updated the code in post no 12
    Last edited by newtolearningjava; April 26th, 2014 at 05:46 PM.

  15. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: dispose(); not working

    So you have the dispose() method just hanging out there as a statement on a line by itself. That means it must be enclosed by a class that includes the dispose() method, AND that it will close the currently executing class, not the previous class.

    My example clearly shows how to pass a reference and then call the dispose() method on the passed reference. You haven't done (and haven't tried to do) anything similar to what I've shown. You apparently aren't calling the dispose() method on an object that has the dispose() method, or you wouldn't be getting the error.

    There are other missteps in your code, but perhaps those are due to translation errors or your attempt to post just enough code to show the problem.

  16. #16
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: dispose(); not working

    omg

    i am confused

  17. #17
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: dispose(); not working

    Then slow down, back up, and simplify.

    Start with your first class - simply get it on screen. From its constructor, create an instance of the second class, passing to it a reference to the first. In the constructor of the second class, call dispose() on the reference passed by the first and make the second visible. Comment all code that does anything else not needed at this point.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: dispose(); not working

    Another way:
    Add a method named: callDispose() to the Game class. Have that method call dispose().
    From the listener method in the inner class, call the callDispose() method.
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    GregBrannon (April 19th, 2014)

  20. #19
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: dispose(); not working

    how do i do this?
    Last edited by newtolearningjava; April 26th, 2014 at 05:47 PM.

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: dispose(); not working

    Take it one step at a time.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #21
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: dispose(); not working

    Very confusing. Why is there a dispose() method in the GameListener class? (By convention, class names in Java begin with capital letters. Please follow Java's naming conventions.) What is it supposed to do? Comments would be helpful (maybe).

  23. #22
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: dispose(); not working

    norm please help me to do this, been trying to solve this for hours
    Last edited by newtolearningjava; April 26th, 2014 at 05:47 PM.

  24. #23
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: dispose(); not working

    Is this a rebadged StressedOut?

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: dispose(); not working

    is this correct?
    What was the name of the method I suggested be added?
    What is the name of the method you added?
    Are they the same?
    That's your answer.
    If you don't understand my answer, don't ignore it, ask a question.

  26. #25
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: dispose(); not working

    [/COLOR]
    Quote Originally Posted by GregBrannon View Post
    Is this a rebadged StressedOut?
    what does mean?
    Last edited by newtolearningjava; April 26th, 2014 at 05:48 PM.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 7
    Last Post: April 25th, 2013, 01:12 PM
  2. Syntax error with dispose()
    By jagnat in forum AWT / Java Swing
    Replies: 1
    Last Post: October 14th, 2011, 11:00 AM
  3. dispose()'ing a JFrame and replacing it
    By musasabi in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 14th, 2010, 02:31 PM
  4. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM