views:

67

answers:

2

Hi Everyone, I have an application which opens multiple JIFs, But I only want to create a single instance of the JIF, so I use these function to check that, and I use dispose to close the JIF after a key is pressed(JDesktopPane.getSelectedFrame().dispose()). However after 2-3 successive disposes, it doesn't create a new JIF? Am I doing anything wrong over here?

public static void setInternalFrame(final JInternalFrame internalFrame) {
    log.debug("CurActiveInternalFrame " + ShoppyPOSApp.getCurrentActiveInternalFrame(), null);
    log.debug("Incoming internalFrame " + internalFrame, null);

    boolean isFrameFound = false;
    try {
        // Have a check whether the DesktopPane contains the internal Frame
        // If yes bring it to front and set the focus
        for (int i = 0; i < ShoppyPOSApp.frame.mainDesktopPane.getAllFrames().length; i++) {
            if (ShoppyPOSApp.frame.mainDesktopPane.getAllFrames()[i].getClass() == internalFrame.getClass()) {
                isFrameFound = true;
            }
        }

        if (!isFrameFound) {
            internalFrame.setVisible(true);
            internalFrame.setLocation(
                ShoppyPOSApp.frame.mainDesktopPane.getWidth()/ 2 - internalFrame.getWidth() / 2,
                ShoppyPOSApp.frame.mainDesktopPane.getHeight() / 2 - internalFrame.getHeight() / 2
            );
            ShoppyPOSApp.frame.mainDesktopPane.add(internalFrame);
        }
        internalFrame.setSelected(true);
    } catch (Exception e) {
        log.debug(e.toString(), null);
    }
}
+1  A: 

I don't think dispose is doing what you mean for it to do. dispose gets rid of the operating system "peer" of your frame. But if you intend to show that frame again, then you shouldn't throw away its underpinnings!

I'd go with setVisible(false) on the JIF to hide it. Then you can re-activate it with setVisible(true).

Carl Smotricz
+1  A: 

You are comparing the classes of your input parameter and your desktops internal frames in your for loop. This will always be true as your parameter is an instance of JInternalFrame and the getAllFrames method returns an array of JInternalFrames. Why not just do a regular comparison? :

ShoppyPOSApp.frame.mainDesktopPane.getAllFrames()[i] == internalFrame

I would recommend using HIDE_ON_CLOSE as your default close operation on the frames and using setVisible(false) in your key listener instead of dispose(). When frames are disposed they are closed and you should not try and reuse a frame after is has been closed. If you just hide the frame it will still be a child of the desktop pane so you shoud add a call to setVisible(true) when you find a frame in your setInternalFrame method.

It sounds like you're getting inconsistent behaviour (you say it fails after two or three disposes). This suggests to me that you have an event thread problem. Is your setInternalFrame being called on the event thread? Are you familiar with the Event Dispatch Thread and are you using it correctly?

Tom Martin