Search code examples
javaswingjinternalframejdesktoppane

How to manage a JInternalFrame calling another JInternalFrame?


I have a JDesktopPane with this code.

public class Menu extends JFrame implements ActionListener{
/**
 * Creates new form Portada
 */
public static JDesktopPane desktop;

public JDesktopPane getDesktop() {
    return desktop;
}

public Menu() {
    desktop = new JDesktopPane();
    setContentPane(desktop);

    desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
    initComponents();
}
}

then i add the new components like this

desktop.add(orden);

and when i want to call them i use

if(e.getSource()==jMenuItem1_1){
        orden.setVisible(true);
        desktop.setSelectedFrame(orden);
        desktop.moveToFront(orden);
        try {
            orden.setSelected(true);
        } catch (PropertyVetoException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

The problem i get is that when "orden" wants to pop out another JInternalFrame i use the next code.

searchSupplier.setVisible(true);
    Main.getInstance().getPortada().getDesktop().add(searchSupplier);
    Main.getInstance().getPortada().getDesktop()
            .moveToFront(searchSupplier);
    try {
        searchSupplier.setSelected(true);
    } catch (PropertyVetoException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

When I execute the event more than 2 times i get the next error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal component position

Where should i add the new JInternalFrame to the DesktopPane? or to Orden?, or What can i do to fix this error?


Solution

  • If the searchSupplier frame is already on the desktop, it is unlikely that you will able to add it again. Try using getParent to determine if the frame needs to be added

    if (searchSupplier.getParent() == null) {
        Main.getInstance().getPortada().getDesktop().add(searchSupplier);
    }
    searchSupplier.setVisible(true);
    Main.getInstance().getPortada().getDesktop().moveToFront(searchSupplier);
    try {
        searchSupplier.setSelected(true);
    } catch (PropertyVetoException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }