Illegalityxception when reopening jInternalFrame several times

Asked

Viewed 178 times

3

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

Demonstração do Erro

Window with Jdesktoppane:

package janelas;

public class desktop extends javax.swing.JFrame {
    private frameInterno register;

    public desktop() {
        initComponents();
    }

    private void initComponents() {

        painelDesktop = new javax.swing.JDesktopPane();
        btnAbrirFrame = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout painelDesktopLayout = new javax.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;

}

Janela Jinternalframe:

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) 
  • +1 for the explanatory video. Can you copy and paste the error (Exception) into your question as well? Can’t quite see the reason for the error

1 answer

2


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

I could not debug the code completely, but I can deduce that this occurs due to the internal frame does not cease to exist, because as already explained, the Disposis does not destroy the instance.


Errata on the explanation of the error

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


To avoid this type of problem and as already explained the functioning of the Disposis() for internal frames, it is better to change the visibility instead of giving Disposis, because the instance will be available anyway, so just change the method of closing the internal frame to:

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

And with this change, it is not necessary to add more than once the desktoppane, then, move the addition inside the 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:

inserir a descrição da imagem aqui


1 - source: class documentation

Browser other questions tagged

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