aspose file tools*
The moose likes Java in General and the fly likes how to delete an object from heap ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to delete an object from heap ?" Watch "how to delete an object from heap ?" New topic
Author

how to delete an object from heap ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

hi ,
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 ?


    The Only way to learn is ...........do!
    Visit my blog http://inaved-momin.blogspot.com/
    Stephan van Hulst
    Bartender

    Joined: Sep 20, 2010
    Posts: 3649
        
      17

    So why not just retrieve the value from the current JTree, and work with it?

    I also fail to see what the topic subject has to do with the question. Are you implying you want to get rid of old JTrees? If so, why don't you just overwrite their references?
    Harsha Smith
    Ranch Hand

    Joined: Jul 18, 2011
    Posts: 287
    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.
    John Jai
    Bartender

    Joined: May 31, 2011
    Posts: 1776
    so when i trying to click a download button that button downloads all the three files ie a , b and c.txt

    How do you determine which file to download in the button's action listener? Are you adding all selected files (a.txt, b.txt, c.txt) in a List in the TreeSelectionListener:valueChanged() method?

    Is the filename variable an instance method of your custom JTree implementation or TreeNode? It might be helpful if you show the TreeSelectionListener and button's actionPerformed() code.
    naved momin
    Ranch Hand

    Joined: Jul 03, 2011
    Posts: 692

    Stephan van Hulst wrote:So why not just retrieve the value from the current JTree, and work with it?

    I also fail to see what the topic subject has to do with the question. Are you implying you want to get rid of old JTrees? If so, why don't you just overwrite their references?

    what you have said that 100% is a solution for my problem , i want to get rid of old jtree's instance how to do that ?
    and what do you mean by overwrite their references ?

    4 more idea
    see the below code of tree selection listener's value changed & action performed for downloading data over ftp server



    naved momin
    Ranch Hand

    Joined: Jul 03, 2011
    Posts: 692

    John Jai wrote:
    so when i trying to click a download button that button downloads all the three files ie a , b and c.txt

    How do you determine which file to download in the button's action listener? Are you adding all selected files (a.txt, b.txt, c.txt) in a List in the TreeSelectionListener:valueChanged() method?

    Is the filename variable an instance method of your custom JTree implementation or TreeNode? It might be helpful if you show the TreeSelectionListener and button's actionPerformed() code.

    have a look at the code which i have shown above
    my problem is
    with a action performed , jtree's instance are getting created and placed in a splitpane , yes there are 2 jtree .

    what i want is how can i get ride of all the previous jtree's instance ?
    John Jai
    Bartender

    Joined: May 31, 2011
    Posts: 1776
    The thread might sit well in the Swing forum.... Is files_Name a static String variable? If not can you try making it like that.

    Other than that I am not able to guess any possible issue with the code given...
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24187
        
      34

    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.


    [Jess in Action][AskingGoodQuestions]
    naved momin
    Ranch Hand

    Joined: Jul 03, 2011
    Posts: 692

    solved
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24187
        
      34

    naved momin wrote:solved


    So what was it?
    naved momin
    Ranch Hand

    Joined: Jul 03, 2011
    Posts: 692

    Ernest Friedman-Hill wrote:
    naved momin wrote:solved


    So what was it?

    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
    naved momin
    Ranch Hand

    Joined: Jul 03, 2011
    Posts: 692

    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
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24187
        
      34

    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.
    naved momin
    Ranch Hand

    Joined: Jul 03, 2011
    Posts: 692

    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

    this is what i want
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: how to delete an object from heap ?