Changing the contents of a panel in JFrame

0

In my application, I have a main screen with a menu. The contents of each menu screen should appear inside a main screen panel, I do not want to open a new screen for every thing.

What I need then is to change the contents of the main screen panel, just like the Android fragments.

I made a picture to make it easier to understand:

I saw some ways to do where I would have to add element by element in panel. I wanted to swap the entire panel and not element by element.

Does anyone know how to do it?

    
asked by anonymous 25.10.2017 / 05:37

1 answer

2

One simple way to do this is to create different panels for each screen, and change as needed. Using CardLayout , the exchange is simpler than having be invalidating and redrawing the screen at all times, or creating internal frames (which I think well not expensive to control than with panels.)

You initially need to create the panels, as if it were the "fragments" of the screen, then name them so that CardLayout can identify them together.

See the example below adapted from the documentation examples, to get a sense of how you can do it:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CardLayoutDemo extends JFrame {

    private static final long serialVersionUID = 1L;
    final static String BUTTONPANEL = "Card with JButtons";
    final static String TEXTPANEL = "Card with JTextField";
    JPanel cards;

    public CardLayoutDemo() {

        setTitle("CardLayoutDemo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenuBar menubar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        JMenuItem submenu01 = new JMenuItem("Painel de botoes");
        submenu01.setActionCommand(BUTTONPANEL);
        submenu01.addActionListener(new ChangeCardlayoutListener());
        JMenuItem submenu02 = new JMenuItem("Painel de texto");
        submenu02.setActionCommand(TEXTPANEL);
        submenu02.addActionListener(new ChangeCardlayoutListener());
        menu.add(submenu01);
        menu.add(submenu02);
        menubar.add(menu);
        setJMenuBar(menubar);

        // cria o painel de botoes
        JPanel card1 = new JPanel();
        card1.add(new JButton("Button 1"));
        card1.add(new JButton("Button 2"));
        card1.add(new JButton("Button 3"));
        // cria o painel de campos de texto
        JPanel card2 = new JPanel(new GridLayout(2, 1, 5, 5));
        card2.add(new JTextField("TextField", 20));
        card2.add(new JTextField("TextField2", 20));

        // este painel é quem será o container para o cardlayout
        // organizar os outros dois paineis
        cards = new JPanel(new CardLayout());
        cards.add(card1, BUTTONPANEL);
        cards.add(card2, TEXTPANEL);

        getContentPane().add(cards, BorderLayout.CENTER);
        pack();
        setVisible(true);
    }

    class ChangeCardlayoutListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent evt) {
            CardLayout cl = (CardLayout) (cards.getLayout());
            cl.show(cards, (String) evt.getActionCommand());
        }
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(CardLayoutDemo::new);
    }
}

Running:

Notethat,obligatorily,oneofthepanelsneedstogetvisible,butIthinkthisisnotaproblem.Andifthepanelsaretoocomplex,theidealistocreateaclassforeachonethatextendsJPanel,andinstantiatesthemwhenaddinginCardLayout.

Reference:

  • How to Use CardLayout
25.10.2017 / 11:47