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.

April 19th, 2014, 12:21 PM

Norm

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.

April 19th, 2014, 01:41 PM

newtolearningjava

Re: dispose(); not working

Quote:

Originally Posted by Norm

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.

April 19th, 2014, 01:46 PM

GregBrannon

Re: dispose(); not working

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

April 19th, 2014, 01:58 PM

newtolearningjava

Re: dispose(); not working

YES Greg i have opened

April 19th, 2014, 02:14 PM

GregBrannon

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:

Code java:

// the first frame in a sequence of displayed framesclass Class1 extendsJFrame{// default Class1() constructorpublic 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 Class2privatevoid transferControlToClass2(){new Class2(this);}}// end class Class1//the second frame in a sequence of displayed framesclass Class2 extendsJFrame{// a reference to a Class1 object// (not used but included to show you how it could be)
Class1 class1;// default Class2() constructorpublic 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.

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.

April 19th, 2014, 02:58 PM

newtolearningjava

Re: dispose(); not working

i cant

April 19th, 2014, 03:05 PM

GregBrannon

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.

April 19th, 2014, 03:13 PM

newtolearningjava

Re: dispose(); not working

Code :

public class game extends JFrame{
//some code
}}

April 19th, 2014, 03:20 PM

Norm

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.

April 19th, 2014, 03:26 PM

newtolearningjava

Re: dispose(); not working

i have updated the code in post no 12

April 19th, 2014, 03:28 PM

GregBrannon

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.

April 19th, 2014, 03:33 PM

newtolearningjava

Re: dispose(); not working

omg

i am confused

April 19th, 2014, 03:44 PM

GregBrannon

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.

April 19th, 2014, 03:48 PM

Norm

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.

April 19th, 2014, 03:54 PM

newtolearningjava

Re: dispose(); not working

how do i do this?

April 19th, 2014, 04:01 PM

Norm

Re: dispose(); not working

Take it one step at a time.

April 19th, 2014, 04:01 PM

GregBrannon

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).

April 19th, 2014, 04:16 PM

newtolearningjava

Re: dispose(); not working

norm please help me to do this, been trying to solve this for hours

April 19th, 2014, 04:17 PM

GregBrannon

Re: dispose(); not working

Is this a rebadged StressedOut?

April 19th, 2014, 04:18 PM

Norm

Re: dispose(); not working

Quote:

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.