Question

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

Was it helpful?

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.

OTHER TIPS

Here is may sample code. hope this help. Menu action to call internal frame in main application where JdesktopPane in it.

 private void YourJinternalFrameMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                                 

    YourJinternalFrame nw = YourJinternalFrame.getInstance();
    nw.pack();
    //usefull part for you.. if open shows, if not creates new one 
    if (nw.isVisible()) {
    } else {
        desktopPane.add(nw);
        nw.setVisible(true);
    }
    try {

        nw.setMaximum(true);
    } catch (PropertyVetoException ex) {
        Logger.getLogger(MainApplication.class.getName()).log(Level.SEVERE, null, ex);
    }
}   

put this inside of your YourJinternalFrame

private static YourJinternalFrame myInstance;

public static YourJinternalFrame getInstance() {
    if (myInstance == null) {
    myInstance = new YourJinternalFrame();
    }
return myInstance;

Try this simple code :

YourJinternalFrame nw = new YourJinternalFrame();

private void YourJinternalFrameMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    if(!nw.isVisible()){
        YourJDesktopPane.add(nw);
        nw.setVisible(true);
    }

}   

Try this simple code take class variable chk and set equal to 0 then call jframe method componentremoved in this set chk =0 again and if you call your internal frame set chk =1 and compare chk on calling internal wheather it is zero or not thats all

I went for a different solution:

final JInternalFrame[] frames = desktopPane.getAllFrames();

if( !Arrays.asList(frames).contains(loginFrame) ) {
    loginFrame = new LoginFrame();
    desktopPane.add(loginFrame);
    loginFrame.setVisible(true);
}

This worked for me (checking for the class name):

final JInternalFrame[] frames = desktopPane.getAllFrames();
LoginFrame loginFrame = new LoginFrame();

if( !Arrays.asList(frames).toString().contains("LoginFrame") ) {
    desktopPane.add(loginFrame);
    loginFrame.setVisible(true);
    loginFrame.validate();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top