javaswingjinternalframejdesktoppane

only open one JInternalFrame in one time


I've created a Swing application with several JInternalFrames which gets added to a JDesktopPane on the event of a mouse click. I want only one instance of the same Internal frame to be present on the DesktopPane. I dont want the same frame to appear twice when the user opens the frame..


Solution

  • The simple solution to your problem is to create an HashMap<String,JInternalFrame>. The key will be the title of that JInternalFrame and value will be the object of that JInternalframe opened currently.Save the (key,value) pair in HashMap when the internal frame is opened first time. Disable the close button for all JInternalFrame window , so that user can't dispose the displayed JInternalFrame window. Register esc key to each JInternalFrame object , so that when esc button of keyboard is pressed the currently display JInternalFrame is minimized on the DesktopPane.Now When you click on menu item to open that same internal frame, check if the title of that JInternalFrame is existing in that HashMap askey. If it exists then retrieve the value for that key and refer it by JInternalFrame variable and then restore the same on DesktopPane. If the corresponding entry of title doesn't exist in that HashMap , create a new JInternalFrame object, make an entry for same in the HasMap and display it.

    Note: Whatever I have posted here is the solution for the situation where you can have many types of JInternalFrame each having unique different functionality, and you want to keep only one instance of each of those JInternalFrame.