6

How can I destroy the Jframe object(like no references should be left) because I am implementing multi user login system to itunes like app so a user can add songs delete songs. I have 3 frames loginFrame, adminFrame, nonAdminFrame.. loginFrame = to login which starts nonAdminFrame where the add deleting songs are taken careof The login is being handled as I have data folder where .txt files are used to write user objects which has the song info type linked lists. The way i login I look into the data folder and see if there is .txt file named user1.txt file and it will load up all data into nonadminFrame... The problem is login is not working properly as it as references to older nonAdminFrame where the previous user data is still present...

I have 3 classes or 3 JFrames. The mainclass is loginFrame. I get the login info and see if the user is admin or nonadmin and then show the admin or nonadminFrame by creating a new adminFrame() object or nonAdminFrame() object and i set loginFrame.setVisible(false); The problem is with nonAdminFrame where the all itunes library stuff happens. I have JTree to show all the songs for that user and once the clicks logout I dispose of the nonAdmin frame using frame.dispose() but if I login again with a different again creating a nonadminFrame() object I see old user's data in the JTree that the problem...

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
jrdnsingh89
  • 215
  • 2
  • 7
  • 18
  • 1
    Perhaps it's me, but I'm greatly confused by your question. If you don't get a decent answer soon, can you elaborate on your problem a bit, show pertinent code, try to explain your code a bit, and tell us more details as to what you're trying to do and what's not working? – Hovercraft Full Of Eels Mar 31 '13 at 19:30
  • @HovercraftFullOfEels Is that you? or a bot posting the same comment over and over? :P – Eng.Fouad Mar 31 '13 at 19:33
  • I will add some details... – jrdnsingh89 Mar 31 '13 at 19:34
  • 2
    @Eng.Fouad: It's a canned reply of course, but I think it's a decent way of requesting more information without being too antagonistic (which is my usual problem!). For instance, it's a lot better than saying "WTF"? – Hovercraft Full Of Eels Mar 31 '13 at 19:35
  • @Eng.Fouad: I also worry about his use of a lot of JFrames as that suggests a possible code smell, but of course we await more information from the OP. – Hovercraft Full Of Eels Mar 31 '13 at 19:37
  • 1
    It sounds like, instead of recreating the frame each time you want to use it, you are using a single reference. Unless you take the time to "clear" all the fields on the frame of their values, they will always appear with the last values they were entered with. You have 2 choices. Re-create the frames each time you need to use them or provide a means for the frame to clear its fields – MadProgrammer Mar 31 '13 at 19:45
  • How do I do either one of them. How can recreate the frames or clear its fields... – jrdnsingh89 Mar 31 '13 at 19:52
  • Recreate the frames when you need them. Instead of doing JFrame frame = new JFrame(); once, you need to do it every time you want to show the frame. As to clearing the fields, that depends on how you've implemented them – MadProgrammer Mar 31 '13 at 21:20
  • Why did you remove most of your question? I've rolled back to your previous quesiton. -1 vote to be removed if you give a good explanation for why you did this. – Hovercraft Full Of Eels Mar 31 '13 at 22:11

4 Answers4

15

Is there a way in your user1.txt file to notice if that user is an administrator or not? Your question isn't very clear, but you should be able to do something like this:

JFrame frame = new JFrame();
frame.dispose();

The compiler will literally dispose of this frame and automatically clean up using the garbage collector.

DerpyNerd
  • 4,743
  • 7
  • 41
  • 92
  • The admin stuff is taken care.. I tried that but it doesn't work. Old data is still there.. – jrdnsingh89 Mar 31 '13 at 19:41
  • This doesn't make sense because the frame and all of it's components and data are destroyed after a dispose. Do you store the user data to another location? Do you use some kind of session manager? – DerpyNerd Mar 31 '13 at 19:48
  • I have to implement session persistance by writing and reading the user data using ObjectInputStream and ObjectOutputStream .. – jrdnsingh89 Mar 31 '13 at 19:51
  • The problem is not your nonadminframe. After disposing the frame, all data is gone forever (theoretically). Try this approach, if you login, dispose of the login frame and recreate it when you log out. I'm not sure what your code looks like so I might as well guess :) – DerpyNerd Mar 31 '13 at 20:16
  • Is the data really gone? a) The documentation for dispose says: "The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show." b) I have a JUnit test that creates a JFrame before a test method begins, and calls dispose() on it after the test method ends. After running 5 test methods I wind up with 5 identical copies of the JFrame in the Frame array (a la Frame.getFrames()). – Jack Straub Aug 14 '18 at 21:10
  • @JackStraub I may mistaken, but doesn't dispose also mean that an item is flagged for disposal until the garbage disposers pass by? It does also mention "rebuilding the native resources" which means that a subsequent call will cause the frame to be rebuild from scratch. I don't have any java code before me, but if I understand correctly, you have 5 individual frames flagged for disposal. Correct me if i'm wrong of course ;) It's been a while since I coded java – DerpyNerd Aug 17 '18 at 13:00
  • @DerpyNerd That used to be my belief. I began questioning it while writing JUnit test drivers for a GUI. After a couple of tests that constructed a dialog, additional tests failed because searching the Frames for a component was returning components from older instantiations (I fixed the problem by removing the JFrame's content pane at the same time that I called dispose()). The docs say that only "native resources" are freed, and the dialog can be restored using the pack() method; I don't see how it could be valid to call a method on an object that has been marked for garbage collection. – Jack Straub Aug 17 '18 at 21:36
  • @JackStraub Doesn't that mean the garbage collector hasn't disposed of the components before you try to find a component? Or maybe any action you perform after disposing the frame has the same effect as .pack()? You should try your test with a `Thread.sleep(4000);` after you called dispose and don't destroy the frame. see what the garbage collector destroys – DerpyNerd Aug 18 '18 at 13:42
  • @DerpyNerd I ran a loop ten times to: display a JFrame; dispose the JFrame; print Frame.getFrames().length; wait 4 seconds. At the end of the loop there were 10 frames. I can send you the code, but I don't know how. – Jack Straub Aug 19 '18 at 16:18
  • @JackStraub Sorry I'm not really into java anymore so I would need to reinstall all my tools to check the code. You want to reset a frame after each iteration? I believe you would have to `frame.dispose(); YourFrame frame = new YourFrame();` in order to remove the reference to the old frame. Again, I'm not doing Java enough anymore to be sure. – DerpyNerd Aug 20 '18 at 10:24
3

This also does the same thing:

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

This will also free up any resources used by the frame, according to the following article: http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html

blackpanther
  • 10,998
  • 11
  • 48
  • 78
3

As shown here, you can't completely reclaim a disposed frame's memory. Instead, create a single frame having a single panel that uses CardLayout to display the login, admin and user panels. An example may be seen here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
3

like others said this should work

JFrame frame = new JFrame();
frame.dispose();

but if you use a singleton pattern and you declare a jframe as a class member

private static jFrame myframe = null ... singleton pattern...
...

you have to add this

myframe.dispose();
myframe = null;
Pablo
  • 104
  • 3