I have array of 10 JFrames instanced and I want to kill 3 or 5 or n if them,but not all!!!

how should I do it?

and where? in constructor or in main...?

public class KlasaMrdanje extends JFrame
{
	 public int kontrola1;
	 
	 	public KlasaMrdanje(int kontrola) throws Exception //KAKO DOBRO SA KONSTRUKTOR ISPALO HEHE
	 	{

	    	//int kontrola=3;
		 	//double[] nizd = new double [nd];
	    	StoProzora[] a=new StoProzora[50]; //kreiras dinamicki vektor
	    	//niz[1].setVisible(true);
	    	
	    	
// bam,samo da ih izbaci	    	
	         for (int i=0;i<10;i++)
	         {
	        	 a[i] = new StoProzora();//kreiras objekte vektora
	        	 a[i].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	        	 
	        	 //for(int j=0;j<(int)Math.random()*100;j++)
	        	 
	        	 //Thread.sleep(5);
	        	 //a[i].setVisible(true);
	             a[i].setLocation((int)(Math.random()* 1000),(int)(Math.random()*1000));
	        	 a[i].setVisible(true);
	         }

Recommended Answers

All 6 Replies

Interesting...

I'll take "kill" as hiding a specific JFrame.

First of all, if you need to know which JFrames are the ones you want to hide. You may implement any of the following methods to keep all your JFrame's identified:
- Use an array ( JFrame[] )
- Use an ArrayList ( ArrayList<JFrame> )
- Give each JFrame a different name that matches with one of your arguments.

Now that you have that done, decide which JFrame you want to hide. For example, if i used the second method, I'd be using:

ArrayList<JFrame> list = new ArrayList<JFrame>(); // Declaration of the JFrame list.
...
int index; // Declaration of the index to be used when you want to hide a JFrame.
...
list.get( index ) // returns a JFrame object
.setVisible( false ) // Turns false "visibility" for the returned JFrame object.

Now, if you need to "kill" the JFrame #3, #4, #6 you may use the following:

// @ <init>
ArrayList<JFrame> list = new ArrayList<JFrame>();
...

/**
* Kills a specific JFrame by using its index.
* @param index The Index of the JFrame you want to "kill".
*/
private void killJFrameByIndex( int index ) {
	if( index >= list.size() ) 
		return; // I don't want to throw an exception here.
	list.get( index ).setVisible( false );
}

/**
 * "Kills" the JFrames #3, #4 and #6
 */
public static void main(String[] args) {
      // Must add all the JFrame's to the [I]list[/I] now.
      ...
      killJFrameByIndex( 3 );
      killJFrameByIndex( 4 );
      killJFrameByIndex( 6 );
}

Hope it helped.

~ Xhamolk_

acctually when I ment Kill I ment destroy the object

as you see I have arrray of objects

StoProzora[] a=new StoProzora[50];

for (int i=0;i<10;i++)
a[i] = new StoProzora();

so I have 10 of them,I want to destroy some of them

what you made for hiding them I'm also going to use,so you helpend and thanx

///////////////////////////////////////////////////////////////////////////////////////

what is on my mind here is to make really big constructor that creates and destroys and control the JFrames whit his big list of input parametars,like this:

public class KlasaMrdanje extends JFrame
{
	
	 		 	public KlasaMrdanje(int controlX,int numberOfFrames,boolean visibilityOfFrames) throws Exception //KAKO DOBRO SA KONSTRUKTOR ISPALO HEHE

vith "controlX" I control moving or minimising frames with while(controlX==1){}
while(cintrolX==2){}

I used whiel() to choose methods practically.Is this the good way? Or there's a better one?

so what I want to ask is this good solution with such a big constructor or should it be done with separated methods???

Java has a garbage collector. You don't need to destroy anything like you do in C++ with the delete command. When it's no longer being used anywhere, Java will get rid of it. You can always set something to null. That means that you are done with the object.

SomeObject object1 = new SomeObject ();
object1.dostuff ();
// done with object1.  Let's "destroy" it.
object1 = null;
// There is now no reference to the SomeObject instance created
// by the constructor in line 1.  The garbage collector will figure that
// out behind the scenes and free the memory when it runs in the
// background.

very nice explanation!!!

it's so much clearer now !!!

bravo!!

My next question is how to count how much intanced objects you have at some moment???

very nice explanation!!!

it's so much clearer now !!!

bravo!!

My next question is how to count how much intanced objects you have at some moment???

Well you may want to consider what Xhamolk_ suggested in post # 2. Have an ArrayList or some other Collection (as opposed to an array) of JFrames. Deleting an element is easy. Just use ArrayList's remove methods. And you can check the size with its size method.

http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html

If you use a 10-element array and set an element to null, that element will be null, but the array length would still be 10. You would have to go through the array and check for null values. Use an ArrayList and it's done for you.

Well you may want to consider what Xhamolk_ suggested in post # 2. Have an ArrayList or some other Collection (as opposed to an array) of JFrames. Deleting an element is easy. Just use ArrayList's remove methods. And you can check the size with its size method.

http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html

If you use a 10-element array and set an element to null, that element will be null, but the array length would still be 10. You would have to go through the array and check for null values. Use an ArrayList and it's done for you.

Option #1.
You may also try to override the size method during the declaration of the ArrayList; for example:

ArrayList<JFrame> list = new ArrayList<JFrame>() {
        @Override
        public int size() {
            int size = 0;		
            try {
                for( JFrame frame : this )
                    if( frame != null )
                        size ++;
                return size;
            }catch( Exception e ) {
                return super.size();
            }
        }

    };

That will solve the problem of having null items getting added to the count.
But cause serious problems, such as:
- Indexing
- Iterating
- Retrieving items from the array.

Option #2.

Creating a sub-class of ArrayList , in which you can create a new method, such as getUsableCount , using the same code as above; but obviously renaming.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.