-2

I'm using Eclipse as my IDE

i already have the code for login -check if user and pass match -check if account's session column in DB is "logged in". if false log in user, else prompt the user

when logging out, i have a log out button which when clicked changes the 'logged in' into 'logged out'. Now the problem is when the user didnt click the log out button and instead just closes the application. I tried making a window listener when the frame is 'closing' then redirecting that to the log out button, it kinda solves my problem so I assigned every frame to redirect to the log out button action when 'window is closing'.

My app works like this: My app has multiple frames. After logging in there is the homepage, then 4 more buttons to direct you to other modules. In Homepage, when you click on module_A, homepage then disposes and module_A frame pops up, if you click the 'back' button module_A disposes and homepage pop ups again, clicking module_b disposes homepage and pops up module_b frame and so on...

Scenerio 1:

~logged in - changes user status from 'logged out' to 'logged in' redirects user from log in page to home page

~on homepage i forgot what i would do so i just close the application, since i have a listener 'window closing' that will change 'logged in' state to 'logged out' it's good.

Scenerio 2:

~logged in - changes user status from 'logged out' to 'logged in' redirects user from log in page to home page

~on homepage i click on module_A, that will then dispose the homepage AND will change 'logged in' status into 'logged out' because homepage window closed.

how can i fix scenerio 2? since closing a frame logs me out but im still using the app only with a different frame called moduleA

PS. if you guys dont understand what im saying, please ask questions, ill answer as fast as i can. im not really good at explaining my situation im so sorry :'(

meepawned
  • 15
  • 5
  • 1
    If I understand correctly you want to close the session when the application exists and currently to determine that you are using the close event of the main frame (which is not good because this event is triggered sometimes even if the application is not exited). A better solution will be to use a [Shutdonw Hook](http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)) – Titus Mar 21 '15 at 03:22

1 Answers1

2

So I guess what you have is something like this:

void onWindowDispose() {
    logMeOut();
}

What I'd suggest is to use something like a counter:

int windowCount;

void onWindowCreate() {
    ++windowCount;
}

void onWindowDispose() {
    --windowCount;

    if(windowCount == 0)
        logMeOut();
}

You might not use exactly a counter, but I hope you get the idea. You should only log out when all of the windows are closed, not just any one of them.

Also, if "multiple frames" means "multiple JFrames", please see "The Use of Multiple JFrames, Good/Bad Practice?"

Community
  • 1
  • 1
Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • I see where you're getting to. The problem is where should i put the logmeout when counter==0? in every class/frame? – meepawned Mar 21 '15 at 11:24
  • I don't know what your program design is like so I really can't give you suggestions beyond what I already have. Perhaps the counter is `static` or enclosed in an object that all the frames have a reference to. You already said you were trying to use a window listener and that seems fine. – Radiodef Mar 22 '15 at 06:07