How do I open a window inside a main Frame?

2

I'm doing a program and I'm creating the layout manually with swing using GridBagLayout and GroupLayout . I would like to know how to open a window within the program I am doing.

For example: When the user clicks the Register Client button, the program will open a window bound to the program in a given space on the main interface, instead of opening a separate screen.

I do not want to create a stand-alone screen for each item, but I wanted a main screen of the program and the entire space inside the main screen is populated with the other subscreen screens. I've given an example of the Corel Draw program to show just what I want and improve your understanding:

    
asked by anonymous 05.02.2016 / 18:39

1 answer

2

You can use JInternalFrame to manage your sign-in screens.

A very simple example. Consider that you have your main screen, MainFrame , which has a menu, something like this:

Theshortcodeneededtocreatethisscreenwouldlooksomethinglikethis:

publicclassMainFrameextendsJFrame{privatestaticfinallongserialVersionUID=-4985498392168006224L;privatefinalJDesktopPanedesktop=newJDesktopPane();publicMainFrame(){super("Tela Principal");

        final int inset = 50;
        final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        final Dimension dimension = new Dimension(screenSize.width / 3, screenSize.height / 2);
        this.setBounds(inset, inset, dimension.width - inset * 2, dimension.height - inset * 2);

        this.setContentPane(desktop);
        this.setJMenuBar(this.createMenuBar());

        desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
    }

    // esse é o cara chamado ao acessarmos o menu, ou no seu caso, clicar no botão :)
    protected void createFrame() {
        final UsuarioInternalFrame frame = new UsuarioInternalFrame();
        frame.setVisible(true);
        desktop.add(frame);
        try {
            frame.setSelected(true);
        } catch (final PropertyVetoException e) {
            // trate a exceção conforme sua necessidade
        }
    }

}

Note: As the intent is not to present the basics of swing, just use JInternalFrame , I will not put how to create the menu, open the main screen, etc. =)

When we go to the Registrations menu - > Users , we'd display a JInternalFrame :

Theclickeventonthemenu,button,etc.,basicallycreatestheinternalframeweneed,thecreateFramemethodofourmainscreen.ThecodeforUsuarioInternalFrameissimple,somethinglikethis:

publicclassUsuarioInternalFrameextendsJInternalFrame{privatestaticfinallongserialVersionUID=-619850559630326110L;publicUsuarioInternalFrame(){super("Usuários", true, true, true, true);

        this.setSize(500, 300);

        this.setLocation(30, 30);
    }
}

So you can have multiple internal frames , you can maximize them:

Minimizeit:

Finally,customizeaccordingtoyourneeds,asIsaid,tomakethesizeofitonlyinthegrayareaofyourCorelexample,termonthesidesofthemenus,atree,etc.Thereareseveralwaystodoit.

Thisisjustabasicexampleofhowyoucanuse JInternalFrame , and the example can be customized to suit your needs. In certain cases you can use JPanel , but if I understand your problem, internal frames will give you more flexibility if you do not have to customize a JPanel a lot.

    
06.02.2016 / 14:55