jrdnsingh89
jrdnsingh89

Reputation: 215

destroy Jframe object

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

Upvotes: 6

Views: 29720

Answers (4)

Pablo
Pablo

Reputation: 104

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;

Upvotes: 3

trashgod
trashgod

Reputation: 205805

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.

Upvotes: 3

blackpanther
blackpanther

Reputation: 11486

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

Upvotes: 3

DerpyNerd
DerpyNerd

Reputation: 4803

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.

Upvotes: 15

Related Questions