IllegalArgumentException when reopening jInternalFrame multiple times

3

I have an application that is validating when the JInternalFrame window is open or closed, but if the window is opened and closed a few times in a row, the console displays an error and does not open JInternalFrame again. >

WindowwithJDesktopPane:

packagejanelas;publicclassdesktopextendsjavax.swing.JFrame{privateframeInternoregister;publicdesktop(){initComponents();}privatevoidinitComponents(){painelDesktop=newjavax.swing.JDesktopPane();btnAbrirFrame=newjavax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);javax.swing.GroupLayoutpainelDesktopLayout=newjavax.swing.GroupLayout(painelDesktop);painelDesktop.setLayout(painelDesktopLayout);painelDesktopLayout.setHorizontalGroup(painelDesktopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0,0,Short.MAX_VALUE));painelDesktopLayout.setVerticalGroup(painelDesktopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0,260,Short.MAX_VALUE));btnAbrirFrame.setText("Abrir Frame");
        btnAbrirFrame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAbrirFrameActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(painelDesktop)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(301, Short.MAX_VALUE)
                .addComponent(btnAbrirFrame)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(painelDesktop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAbrirFrame)
                .addGap(0, 11, Short.MAX_VALUE))
        );

        pack();
    }

    private void btnAbrirFrameActionPerformed(java.awt.event.ActionEvent evt) {                                              
        if (register == null) {
            register = new frameInterno();
        }

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

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new desktop().setVisible(true);
            }
        });
    }

    private javax.swing.JButton btnAbrirFrame;
    private javax.swing.JDesktopPane painelDesktop;

}

JInternalFrame window:

package janelas;

public class frameInterno extends javax.swing.JInternalFrame {

    public frameInterno() {
        initComponents();
    }

    private void initComponents() {

        btnSair = new javax.swing.JButton();

        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(300, Short.MAX_VALUE)
                .addComponent(btnSair)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(200, Short.MAX_VALUE)
                .addComponent(btnSair)
                .addContainerGap())
        );

        pack();
    }

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {                                         
        this.dispose();
    }

    private javax.swing.JButton btnSair;
}

Error (exception):

Exception in thread "AWT-EventQueue-0"
java.lang.IllegalArgumentException: illegal component position  at
java.awt.Container.addImpl(Container.java:1100)     at
javax.swing.JLayeredPane.addImpl(JLayeredPane.java:231)     at
javax.swing.JDesktopPane.addImpl(JDesktopPane.java:484)     at
java.awt.Container.add(Container.java:417)  at 
janelas.desktop.btnAbrirFrameActionPerformed(desktop.java:63)   at
janelas.desktop.access$000(desktop.java:3)  at 
janelas.desktop$1.actionPerformed(desktop.java:31)  at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)    at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)    at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)     at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)  at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)  at
java.awt.Component.processMouseEvent(Component.java:6533)   at
javax.swing.JComponent.processMouseEvent(JComponent.java:3324)  at
java.awt.Component.processEvent(Component.java:6298)    at
java.awt.Container.processEvent(Container.java:2236)    at
java.awt.Component.dispatchEventImpl(Component.java:4889)   at
java.awt.Container.dispatchEventImpl(Container.java:2294)   at
java.awt.Component.dispatchEvent(Component.java:4711)   at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)  at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)   at
java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)   at
java.awt.Container.dispatchEventImpl(Container.java:2280)   at
java.awt.Window.dispatchEventImpl(Window.java:2746)     at
java.awt.Component.dispatchEvent(Component.java:4711)   at
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)  at
java.awt.EventQueue.access$500(EventQueue.java:97)  at
java.awt.EventQueue$3.run(EventQueue.java:709)  at 
java.awt.EventQueue$3.run(EventQueue.java:703)  at
java.security.AccessController.doPrivileged(Native Method)  at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)     at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)     at
java.awt.EventQueue$4.run(EventQueue.java:731)  at
java.awt.EventQueue$4.run(EventQueue.java:729)  at 
java.security.AccessController.doPrivileged(Native Method)  at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)     at
java.awt.EventQueue.dispatchEvent(EventQueue.java:728)  at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)   at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)  at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)   at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)   at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)    at
java.awt.EventDispatchThread.run(EventDispatchThread.java:82) 
    
asked by anonymous 22.05.2018 / 22:58

1 answer

3

In short, the error occurs because a component that JDesktopPane is already added is being added. But in the case of your code, there is a dispose() in the inner frame, and this inner frame method makes it invisible, not selectable, and closed 1 , which should remove it from the JDesktopPane .

I have not been able to debug the code completely, but I can deduce that this occurs because the inner frame does not exist, because as already explained, dispose does not destroy the instance.

Errata about error explanation

  What actually causes the error is that dispose() does not destroy the reference to the inner frame, only makes it non-displayable, and the component manager of JDesktopPane , despite removing it a first time, does not repeat this action in the second, which generates the exception because the component is already included, and you try to include it again.

In order to avoid this type of problem and as already explained the operation of dispose for internal frames, it is better to change the visibility instead of giving dispose , since the instance will be available anyway, so just change the method of closing the inner frame to:

private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {  
    this.setVisible(false);
}

And with this change, you do not have to add the component in desktoppane more than once, then move the addition into if:

private void btnAbrirFrameActionPerformed(java.awt.event.ActionEvent evt) {                                           
    if (register == null) {
        register = new frameInterno();
        painelDesktop.add(register);    
    }

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

With this, the error no longer occurs:

1-source: class documentation

    
23.05.2018 / 03:24