i have a situation in my code where i need to delete an object
because , my problem is
i have a jtree , which is getting created each time user click a button
like i m doing this because of my requirements
so each jtree object has which gives me what i have selected at tree selection listener's event
so suppose i have clicked a button 3 times so as a result i have 3 instance of jtree and each jtree object has fileName instance variable
so now assume in
1st instance of jtree filename is a.txt
in 2nd its b.txt
and in 3rd it is c.txt
so when i trying to click a download button that button downloads all the three files ie a , b and c.txt
but i dont want that to happen
what i want is that the only file to get downloaded which is there is fileName and that too the fileName which is in current instance of jtree and my code should ignore previous instance of the same jtree
can any one help me on this please ?
Go to the heap directory. Right click on the desired object and click delete.
Seriously, I don't think there is way to manually delete objects on the heap. When an object can no longer be accessed any live thread, it becomes eligible for garbage collection and the garbage collector may act on the object to reclaim the memory.
The issue is, presumably, that there's a copy of the ActionListener attached to that button for each tree that is created. When the button is pressed, there are three ActionListeners and all of them get invoked. Each one operates on the tree it's connected to. It doesn't matter that the trees aren't visible; because they're referenced from the ActionListeners, they stay around.
What you need to do is call removeActionListener() on the button when each tree is discarded, passing as an argument the same object you passed when you called addActionListener() originally.
hey still there is a tiny bug , will not affect any one but there is a bug
let me explain you what i have done
i have place a button.addactionlistener() on the tree's selection listener's valueChanged()
and removing this with removeactionlistener() after downloading a single file ..so in this way user can download multiple files from cwd
but here is a bug
what when user have just selected the node , it means that actionlistener() is now registered with the button and ready to accept the event
and with this getUserObject() has what user have selected , but not yet downloaded ,
so if that file is not downloaded and user changes the directory , and try to download a file in that directory then a bug arises
which means 2 files will be downloaded one the current file in the cwd and 2nd the file which user have just selected but not downloaded in previous directory
so in this case the 2nd file is empty in front of user's eye ...and that is the BUGGGG
do you have any alternative to solve this ...?
i m trying please if you got any idea let me know ???
and thanks for the removeactionlistener() this was something i have not thought k will solve my problem
but still there is a bug , a tiny bug
What you need to do is call removeActionListener() on the button when each tree is discarded
your this line is superb , i mean the same thing i m trying to do , but the only problem is i dont know when the tree will be discarded , which means
there are 2 ways
1 user might just select the node but not download any file in cwd and change the directory (tree is discarded) (because of this there is a bug)
2 user will select as well download any file from cwd & then changed the directory (i have achieved this)
and what will happen from the 1st way i have explain above please go through
author and iconoclast
It sounds like instead of adding and removing ActionListeners when trees come and go, you should just have one, single ActionListener. This ActionListener should call a method to get "the current tree", and operate on that tree, rather then having a member variable that includes the tree. When your trees come and go, instead of creating and removing ActionListeners, they can set "the current tree". This method might just return the value of a variable, and the trees might simply set the variable to point to themselves when they are created. That way there would never be more than one active tree.
Ernest Friedman-Hill wrote:It sounds like instead of adding and removing ActionListeners when trees come and go, you should just have one, single ActionListener. .
if i would have a single action listener than assume 5 instance of jtree has been created each has there action listener active and registered so when i press button for downloading data on 5th instance of jtree all 4 earlier jtree's action listener will also get triggered
This ActionListener should call a method to get "the current tree", and operate on that tree, rather then having a member variable that includes the tree.
let me tell you one thing the action listener is registered with pop up menu which has a menu item ie " download " and has an implementation of downloading data
jtree has tree selection listener which just capture what user have selected at last point in time
When your trees come and go, instead of creating and removing ActionListeners, they can set "the current tree". This method might just return the value of a variable, and the trees might simply set the variable to point to themselves when they are created.
how to do this ?
any example's ?
That way there would never be more than one active tree