# A quick and hopefully easy JAVA question

**URL:** <https://boards.straightdope.com/t/a-quick-and-hopefully-easy-java-question/100358>\
**Category:** Factual Questions\
**Created:** [March 27, 2002, 11:33am UTC](https://boards.straightdope.com/t/a-quick-and-hopefully-easy-java-question/100358 "2002-03-27T11:33:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Silentgoldfish](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@Silentgoldfish](https://boards.straightdope.com/u/Silentgoldfish)\
**Post date:** [March 27, 2002, 11:33am UTC](https://boards.straightdope.com/t/a-quick-and-hopefully-easy-java-question/100358/1 "2002-03-27T11:33:23Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![TokenFemale](https://avatars.discourse-cdn.com/v4/letter/t/b3f665/32.png) [@TokenFemale](https://boards.straightdope.com/u/TokenFemale)\
**Post date:** [March 27, 2002, 1:37pm UTC](https://boards.straightdope.com/t/a-quick-and-hopefully-easy-java-question/100358/2 "2002-03-27T13:37:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Silentgoldfish](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@Silentgoldfish](https://boards.straightdope.com/u/Silentgoldfish)\
**Post date:** [March 27, 2002, 2:10pm UTC](https://boards.straightdope.com/t/a-quick-and-hopefully-easy-java-question/100358/3 "2002-03-27T14:10:55Z")

</div>

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.

Here’s hoping I made sense…

---

<div class="post-metadata">

**Author:** ![TokenFemale](https://avatars.discourse-cdn.com/v4/letter/t/b3f665/32.png) [@TokenFemale](https://boards.straightdope.com/u/TokenFemale)\
**Post date:** [March 27, 2002, 2:34pm UTC](https://boards.straightdope.com/t/a-quick-and-hopefully-easy-java-question/100358/4 "2002-03-27T14:34:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Silentgoldfish](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@Silentgoldfish](https://boards.straightdope.com/u/Silentgoldfish)\
**Post date:** [March 27, 2002, 3:09pm UTC](https://boards.straightdope.com/t/a-quick-and-hopefully-easy-java-question/100358/5 "2002-03-27T15:09:13Z")

</div>

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 &lt; 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 &lt; 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 :).

---

<div class="post-metadata">

**Author:** ![TokenFemale](https://avatars.discourse-cdn.com/v4/letter/t/b3f665/32.png) [@TokenFemale](https://boards.straightdope.com/u/TokenFemale)\
**Post date:** [March 27, 2002, 4:00pm UTC](https://boards.straightdope.com/t/a-quick-and-hopefully-easy-java-question/100358/6 "2002-03-27T16:00:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![scotth](https://avatars.discourse-cdn.com/v4/letter/s/b9e5f3/32.png) [@scotth](https://boards.straightdope.com/u/scotth)\
**Post date:** [March 27, 2002, 4:56pm UTC](https://boards.straightdope.com/t/a-quick-and-hopefully-easy-java-question/100358/7 "2002-03-27T16:56:28Z")

</div>

I would also expect something like

CircleFigure myCF = CircleFigure.create();

And then do the rest of the work from myCF.

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.

---

<div class="post-metadata">

**Author:** ![TokenFemale](https://avatars.discourse-cdn.com/v4/letter/t/b3f665/32.png) [@TokenFemale](https://boards.straightdope.com/u/TokenFemale)\
**Post date:** [March 27, 2002, 5:11pm UTC](https://boards.straightdope.com/t/a-quick-and-hopefully-easy-java-question/100358/8 "2002-03-27T17:11:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Silentgoldfish](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@Silentgoldfish](https://boards.straightdope.com/u/Silentgoldfish)\
**Post date:** [March 27, 2002, 6:01pm UTC](https://boards.straightdope.com/t/a-quick-and-hopefully-easy-java-question/100358/9 "2002-03-27T18:01:17Z")

</div>

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