saragarcia6123
saragarcia6123

Reputation: 38

Switching between JPanels on a JFrame

At the moment just for testing purposes I would like to change the JPanel on the JFrame every time it is clicked. I am cycling through 3 panels/pages each with their unique background color.

Currently it will change but only paint when it comes to the very first panel that was added otherwise display the default gray.

Code:

@Override
    public void mouseClicked(MouseEvent e) {
        mainFrame.changePage();
    }

public void changePage() {
    if(currentPage != null) {
        this.remove(currentPage);
    }
    currentPage = pages[pageNumber];
    this.add(currentPage);
    currentPage.setPage();
    super.repaint();
    pageNumber++;
    if(pageNumber > 2) {
        pageNumber = 0;
    }
}

Upvotes: 1

Views: 170

Answers (2)

Meet K.
Meet K.

Reputation: 499

EDIT: Since @camickr mentioned my code below does not adhere to best practices, please refer the following link on how to use the CardLayout.

How to use CardLayout - The Java Tutorials

Associated Source


just for testing purposes

Keeping emphasis on the above, here's some sample code. This is NOT production grade, just a POC without needing the CardLayout mentioned by @camickr.

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class FirstSwingExample {  
    
    public static void main(String[] args) {  
        Swinger s = new Swinger();
    }
  
}

class Swinger implements ActionListener {
    
    int currFrame = 1;
    JFrame f;
    JLabel lblPanelOne, lblPanelTwo, lblPanelThree;
    JPanel panelOne, panelTwo, panelThree;
    JButton b;
    
    public Swinger() {
        setup();
    }
    
    private void setup() {
        
        f=new JFrame();
        
        panelOne = new JPanel();
        lblPanelOne = new JLabel("Panel One");
        panelOne.setBounds(0,0,100, 40);
        panelOne.setBackground(Color.BLACK);
        panelOne.add(lblPanelOne);
        
        
        panelTwo = new JPanel();
        lblPanelTwo = new JLabel("Panel Two");
        panelTwo.setBounds(0,0,100, 40);
        panelTwo.setBackground(Color.BLUE);
        panelTwo.add(lblPanelTwo);
        
        panelThree = new JPanel();
        lblPanelThree = new JLabel("Panel Three");
        panelThree.setBounds(0,0,100, 40);
        panelThree.setBackground(Color.GREEN);
        panelThree.add(lblPanelThree);
        
        b=new JButton("Panel Two");
        b.setBounds(130,100,100, 40);
        b.setActionCommand("click");
        b.addActionListener(this);
        
        panelOne.setVisible(true);
        f.add(panelOne);
        f.add(b);

        f.setSize(400,500);  
        f.setLayout(null);  
        f.setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
        if(e.getActionCommand().equals("click")) {
            switch(currFrame) {
                case 1:
                    panelOne.setVisible(false);
                    f.remove(panelOne);
                    panelTwo.setVisible(true);
                    f.add(panelTwo);
                    b.setText("Panel Three");
                    currFrame++;
                    break;
                
                case 2:
                    panelTwo.setVisible(false);
                    f.remove(panelTwo);
                    f.add(panelThree);
                    panelThree.setVisible(true);
                    b.setText("Panel One");
                    currFrame++;
                    break;
            
                default:
                    panelThree.setVisible(false);
                    f.remove(panelThree);
                    f.add(panelOne);
                    panelOne.setVisible(true);
                    b.setText("Panel Two");
                    currFrame = 1;
                    break;
            }
        }
    }
}

Upvotes: -2

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147124

Other than the comment from @camickr to use CardLayout, you need to revalidate. Change:

super.repaint();

to

revalidate();

Upvotes: 3

Related Questions