Hi,
Aim is to close previou
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.
Hi,
Aim is to close previou
Last edited by newtolearningjava; April 26th, 2014 at 05:43 PM.
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.
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.
I thought you were asking about closing one other frame. Why the array? And what do you mean by "smoothly?"
YES Greg i have opened
Last edited by newtolearningjava; April 26th, 2014 at 05:45 PM.
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:
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.// 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
cannot find symbol symbol:
Last edited by newtolearningjava; April 26th, 2014 at 05:46 PM.
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.
i cant
Last edited by newtolearningjava; April 26th, 2014 at 05:46 PM.
Don't make it difficult. Post the code that throws the error; the code that calls the dispose() method, preferably that whole class.
public class game extends JFrame{ //some code }}
Last edited by newtolearningjava; April 26th, 2014 at 05:46 PM.
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.
i have updated the code in post no 12
Last edited by newtolearningjava; April 26th, 2014 at 05:46 PM.
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.
omg
i am confused
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.
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.
GregBrannon (April 19th, 2014)
how do i do this?
Last edited by newtolearningjava; April 26th, 2014 at 05:47 PM.
Take it one step at a time.
If you don't understand my answer, don't ignore it, ask a question.
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).
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.
Is this a rebadged StressedOut?
What was the name of the method I suggested be added?is this correct?
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.