0

Two + 1 Questions:

  1. I've an stand alone application and created a runnable jar for it. Now, when I double-click it, a JFrame window opens and I need to log in. The problem is that I can open multiple JFrame windows if I double-click on it. What I need is once I have logged in, anytime I try to open it again, it should show me the currently logged in window. Or to put it this way, create a single instance of that JFrame window.

  2. When closing the window, I need to do some operations before closing. I know that I can use a WindowListener but this does not work when I shutdown the JFrame (as in terminate the application). Is there a way to do any action before terminating manually?

EDIT : Another question:

  1. I did a setExtendedState(JFrame.ICONIFIED); for minimizing the JFrame window when 'x'(close) is clicked. It minimizes but vanishes from the task bar. Is there a way to keep in the task bar like how the normal minimize works?
Mercenary
  • 2,106
  • 6
  • 34
  • 43
  • for your 3rd question take a look at [here](http://stackoverflow.com/questions/3965336/how-to-minimize-a-jframe-window-from-java) because iconified state makes an icon on the task bar C: also check [this other link](http://stackoverflow.com/questions/7737751/jbutton-minimizing-a-windowjframe) – Frakcool Mar 31 '14 at 18:02
  • I do not see any icon when I click 'x'. It minimizes but vanishes from the task bar. The application will still be running. – Mercenary Mar 31 '14 at 19:17
  • have you tried clicking on the little arrow next to the clock? maybe there's where it is – Frakcool Mar 31 '14 at 20:35

1 Answers1

3

1)

Have your main method search for another instance of the program. If one is found, focus that window and have it close itself. You can read more about that in a similar question here: Question

2)

Use a shutdown hook:

Runtime.getRuntime().addShutdownHook(new Thread() {

    @Override
    public void run() {...} //your code to run when closing the program

}
Community
  • 1
  • 1
Rogue
  • 11,105
  • 5
  • 45
  • 71
  • Thanks. That was what I was searching for! `ShutDownHook`. And thanks for the link. Will take a look! – Mercenary Mar 31 '14 at 17:20
  • For 1) it worked with the code given in the link. Worked when I said is that there is no new window that opens. But, how will I get the already opened window to come into focus. I can fetch the processid/processname but can I use this to do it? – Mercenary Apr 01 '14 at 11:57
  • Hm, Not entirely certain. I suppose you could be tricky with something like sockets, but that kind of cross-process communication starts to get tricky with Java. – Rogue Apr 01 '14 at 13:32
  • Hmm I tried `addShutdownHook`, and when I try to terminate from eclipse, it does not run! Is there anything else to be done? – Mercenary Apr 01 '14 at 15:37
  • `kill -9` or forcible closure of the program won't run shutdown hooks, which I imagine eclipse's environment might do. You should try running it separate from the IDE – Rogue Apr 01 '14 at 15:56