How to check if a JInternalFrame is already open in JDesktopPane?

0

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

This is the code executed when clicking the corresponding item in JMenu:

private void jMenuItemRegisterEmployeeActionPerformed(java.awt.event.ActionEvent evt) {                                                          
    RegisterEmployeeJInternalFrame register = new RegisterEmployeeJInternalFrame();
    jDesktopPaneMain.add(register);
    register.setVisible(true);
} 
    
asked by anonymous 11.06.2017 / 01:37

1 answer

2

In your main class (that of the Frame that accommodates JDesktopPane ), create a variable in this class relative to the inner frame:

private RegisterEmployeeJInternalFrame register;
[...]

When the menu option is selected, check if this frame is visible, if it is not, just make it visible and re-read in JDesktopPane , because if your JInternalFrame is "closable" 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();
} 

In this way, 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() / p>     

11.06.2017 / 02:26