How can I replace one JPanel with another JPanel at the same position?

I have a JFrame with four frames. After this step, I want to replace one panel with another in the same position. My method of switching them is as follows (an object this

is a class that extends JFrame

):

 public void switchPanel(){
    mainPanel.remove(introPanel);
    mainPanel.add(questionPanel);
    this.repaint();
 }

      

The original window creation looks like this:

 mainPanel.add(titlePanel);
 mainPanel.add(sidePanel);
 mainPanel.add(introPanel);

      

+3


source to share


2 answers


You're asking:

How can I replace one JPanel with another JPanel at the same position?

Very easy: use CardLayout as this tool was created for this very situation.

If you had the following constants:

public static final String INTRO_PANEL = "intro panel";
public static final String QUESTION_PANEL = "question panel";

      

and added your JPanel like so



mainPanel.setLayout(cardLayout);
mainPanel.add(introPanel, INTRO_PANEL);
mainPanel.add(questionPanel, QUESTION_PANEL);

cardLayout.show(mainPanel, INTRO_PANEL);

      

Then you can exchange JPanels for:

cardLayout.show(mainPanel, QUESTION_PANEL);

      

would be all that was needed to show the swap, assuming QUESTION_PANEL is the String constant that was used when adding the questionnaire to mainPanel and that mainPanel is using CardLayout.

For example:

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.*;

public class SwapPanels extends JPanel {
   public static final String INTRO_PANEL = "intro panel";
   public static final String QUESTION_PANEL = "question panel";
   private static final int PREF_W = 500;
   private static final int PREF_H = 400;
   private CardLayout cardLayout = new CardLayout();
   private JPanel introPanel;
   private JPanel questionPanel;
   private Random random = new Random();

   public SwapPanels() {
      introPanel = createPanel("Introduction");
      questionPanel = createPanel("Question");
      setLayout(cardLayout);
      add(introPanel, INTRO_PANEL);
      add(questionPanel, QUESTION_PANEL);

      int delay = 3 * 1000; // show intro for 3 seconds
      new Timer(delay, new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            cardLayout.show(SwapPanels.this, QUESTION_PANEL);
            ((Timer) e.getSource()).stop();
         }
      }).start();
   }

   private JPanel createPanel(String title) {
      int rgb = random.nextInt();
      Color color = new Color(rgb);
      JPanel panel = new JPanel(new GridBagLayout());
      panel.setBorder(BorderFactory.createLineBorder(color, 60));
      panel.add(new JLabel(title));
      return panel;
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      SwapPanels mainPanel = new SwapPanels();

      JFrame frame = new JFrame("SwapPanels");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

      

+7


source


after the delete method, you need to run the validate method so that everything. The code will look like this:

public void switchPanel(){
    mainPanel.remove(introPanel);
    mainPanel.add(questionPanel);
    mainPanel.validate();
}

      



Hope this helps. Salam

0


source







All Articles