How to check if a Jinternalframe is already open in Jdesktoppane?

Asked

Viewed 657 times

0

How to check whether a JInternalFrame is already open in the JDesktopPane and, if so, put him in focus on others? I have tried several ways I found searching, but always opens a new frame, even if it is already open.

This is the code executed by clicking on the corresponding item in Jmenu:

private void jMenuItemRegisterEmployeeActionPerformed(java.awt.event.ActionEvent evt) {                                                          
    RegisterEmployeeJInternalFrame register = new RegisterEmployeeJInternalFrame();
    jDesktopPaneMain.add(register);
    register.setVisible(true);
} 

1 answer

3


In its main class (that of the Frame that accommodates the JDesktopPane), create a na variable in this internal frame class:

private RegisterEmployeeJInternalFrame register;
[...]

When the menu option is selected, check that this frame is visible, if it is not, just make it visible and readition on JDesktopPane, for if your JInternalFrame for "closable" (can be closed), it is removed from the desktoppane when closed.

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

    if(register == null ){
        register = new RegisterEmployeeJInternalFrame();
    }

    if(!register.isVisible()){
        dtp.add(register);
        register.setVisible(true);
    }

    register.toFront();
} 

Thus, if the screen has not yet been instantiated and opened, it will be created, and if it has already been instantiated and closed, it will be reopened and launched in front of other internal frames, thanks to toFront().

  • It worked! Perfect! Thank you very much, Diego!

  • @Lucas dispose! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.