How to open a window inside a main frame?

Asked

Viewed 2,691 times

2

I’m doing a show and I’m creating the layout manually with swing using GridBagLayout and GroupLayout. I wonder how to open a window inside the program I’m doing.

For example: When the user clicks the button Cadastrar Customer, the program will open a docked window in the program in a specified space on the main interface, instead of opening a separate screen.

I don’t want to create a separate screen for each item, but I wanted a main screen of the program and the entire space inside the main screen to be filled with the other sub-screens of entries. I posted an example of Corel Draw program to show just what I’m looking for and improve your understanding:

Postei um exemplo do programa Corel Draw para mostrar apenas o que estou querendo e melhorar o entendimento de vocês

  • Ah, I read it better, just add a jpanel there, and set it when you click the button. Instead of creating multiple windows (Jframes), you create containers (Jpanel), each for a different screen. Then, just alternate the visibility of each one according to the call of the buttons.

  • You can use JInternalFrame. In certain cases you can use JPanel, but if I understand your problem, Internal frames will give you more flexibility if you can not have to customize a JPanel

1 answer

2


You can use JInternalFrame to manage your registration screens.

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

Tela Principal

The summary code needed to create this screen would be more or less this:

public class MainFrame extends JFrame {

    private static final long serialVersionUID = -4985498392168006224L;

    private final JDesktopPane desktop = new JDesktopPane();

    public MainFrame() {
        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
        }
    }

}

Obs.: as the intention is not to present the basics of swing, only the use of JInternalFrame, I will not put how to create the menu, open the main screen, etc. =)

As we go on the menu Cadastrals -> Users, we would show a JInternalFrame:

Tela do Usuário ao Abrir

The menu click event, button, etc., basically creates the Internal frame that we need, the method createFrame, of our main screen. The code of UsuarioInternalFrame It’s simple, something like that:

public class UsuarioInternalFrame extends JInternalFrame {

    private static final long serialVersionUID = -619850559630326110L;

    public UsuarioInternalFrame() {
        super("Usuários", true, true, true, true);

        this.setSize(500, 300);

        this.setLocation(30, 30);
    }
}

Then you can have several Internal frames, can maximize them::

Tela do Usuário Maximizada

Minimize:

Tela do Usuário Minimizada

Finally, to customize according to your needs, as you said, to make her size stay only in the gray area of your example of Corel, term on the side menus, a Tree, etc. There are several ways to do.

This is just a basic example of how you can use JInternalFrame, can evolve the example, customize as your need. In certain cases you can use JPanel, but if I understand your problem, Internal frames will give you more flexibility if you can not have to customize a JPanel.

Browser other questions tagged

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