Swap the contents of a panel in Jframe

Asked

Viewed 1,113 times

0

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

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

I made an image to facilitate understanding:

inserir a descrição da imagem aqui

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

Someone knows how to do?

  • Didn’t you ever try anything? There’s something already done from your screen?

  • Yes, I did get to do something. But I could only change the entire screen contents or remove all fields from the panel and add one by one.

1 answer

2


A simple way to do this is by creating different panels for each screen, and switching as needed. Using CardLayout, the switch becomes simpler than having to invalidate and redesign the screen every hour, or creating internal frames(what I find very expensive to control than with panels).

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

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

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);
    }
}

Working:

inserir a descrição da imagem aqui

Note that, obligatorily, one of the panels needs to start visible, but I believe that this is not a problem. And if the panels are too complex, the ideal is to create a class for each one that extends JPanel, and instances them when adding in CardLayout.


Reference:

  • With this I can separate the panels into classes?

  • @Jônatastrabucobelotti see the last paragraph of the answer.

  • Great. I’ll try here, I’ll tell you the result.

  • 1

    Man, it worked perfectly. Thank you so much!

Browser other questions tagged

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