That’s nonetheless driving me up the friggin wall.
How do you get a window that’s been opened in a program to automatically close once the program’s complete?
To make the question a tad longer, I’ve written a program that repeats, but everytime it repeats it opens a new window – easiest way I’d imagine to fix it is to get the window to close before the program hits the repeat. But I can’t figure out how to do it!
It depends on what exactly these windows are. Are they JFrames or JDialogs?
If they are swing components then setVisible(false) called on the variable representing the window should make them invisible, then set the variable to null so the garbage collector will delete them.
If it is an AWT component I don’t know offhand, but if you tell me the type I may be able to help you.
I think they’re JFrames, but not sure… I am rather new at this as I’ll now prove :
I’m pretty sure the setVisible(false) will work cause the variable opening the window is from a package, and in the package as part of opening the window it has window.setvisible(true).
The variable is for drawing a circle and is CircleFigure. I can’t figure out how to work the setVisible(false) into the program and get it past compiling.
Hmm, I can’t really picture where you’re going wrong.
If you posted the piece of code where you declare your CircleFigure, and where you’re trying to make it invisible along with the compiler error I may be able to help you. Is the doumentation on Circle Figure available on the web? I can have a look at that for you if you want.
What the hey, here’s the whole program. It’s not exactly long.
The entire thing compiles and runs as is, to finish it off I’ve got to make the whole thing repeat indefinatly.
import genesis.;
import java.awt.;
public class FloatingBalloons {
public static void main (String args) {
int randomNumber1;
randomNumber1 = (int)(340*Math.random());
while (randomNumber1 < 50) {
randomNumber1 = (int)(340*Math.random());
}
int randomNumber2;
randomNumber2 = 0;
int yCoordinate;
yCoordinate = 220;
CircleFigure.create();
CircleFigure.moveTo(randomNumber1,yCoordinate);
int rand;
rand = (int)(5*Math.random());
if (rand == 0) CircleFigure.setColour(Color.red);
else if (rand == 1) CircleFigure.setColour(Color.blue);
else if (rand == 2) CircleFigure.setColour(Color.green);
else if (rand == 3) CircleFigure.setColour(Color.magenta);
else CircleFigure.setColour(Color.yellow);
while (randomNumber2 < 40) {
randomNumber2=randomNumber2 + 1;
CircleFigure.setRadius(randomNumber2);
Delay.milliseconds(20);
}
while (yCoordinate != -40) {
yCoordinate = yCoordinate - 2;
CircleFigure.moveTo(randomNumber1,yCoordinate);
Delay.milliseconds(20);
}
}
}
As far as I know it’s around the end I want the window to close, so I can put the whole thing into a loop and then I think finish the damn thing :).
I’m afraid I can’t help you with this specific thing as you use a package which does not seem to be documented at all on the web, if it is then please post the URL. The best advice I can give you is to try to find the documentation for the genesis package and try to find a method in the CircleFigure class which will destroy the window, probably something similar to CircleFigure.destroy().
I am sure the .create() returns a handle to an instance of a CircleFigure.
When you are ready to get rid of it, I would look for a .destroy() method or (you have to have an instance handle like myCF above for this to work) you can try myCF = null; and see if goes away.
I have considered that also, but look at the other calls in the program, they are all static calls. I think this class works with static data, so having a handle to an instance would probably do more harm than good.
Had an epiphany; instead of destroying the window I moved the CircleFigure.create() to the top of the program and put a while loop underneath it: problem solved and program works!
Thanks for your help, it’s actually helped me learn a fair bit, what with mucking around with the genesis package.