Home > Mobile >  Java - Check if a jframe is already open?
Java - Check if a jframe is already open?

Time:04-10

I am creating a jframe in java. But I want to check if a jframe exists already on the system. I have tried a method where you get all processes from the task manager and see if it is running but it won't work. I was using a method from this post: https://stackoverflow.com/a/19005828/15349408 The process name of it is "javaw.exe".

It can't check if the jframe object is null because it will be run from a different program. Is it possible to check if a jframe is already open my system.

CodePudding user response:

Sounds like you want a java process to have access to the java objects of other processes. You can do this via IPC, see this answer for some ideas.

If you need this for a single use case only, just to check the existence of one particular JFrame, then i suggest to just use a simple lock file. That file name could be anything, like "myprogram.lock". The only important thing is to make sure that all program instances use the same directory, eg. the Windows temp directory.

  • Right before creating the JFrame, check if the lock file exists.
  • If the lock file already exists, don't create the JFrame.
  • If the lock file doesn't already exist, create the lock file and then create the JFrame.
  • When the JFrame is being closed, delete the lock file.

For debugging purpose you can write the process id into the lock file.

  • Related