-1

How can I replace a JPanel or JFrame & its contents with another one by a simple button click in the same container ?

Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70
Ahmad Karimi
  • 1,285
  • 2
  • 15
  • 26

2 Answers2

0

The trick would be to use CardLayout or modify panel visibility. Please look at the following example that modifies panel visibility.

public class PanelExample {
private JPanel _myPanel1,_myPanel2;
public void init() {
    JFrame frame = new JFrame("Testing");
    JPanel mainPanel = new JPanel(new GridBagLayout());

    _myPanel1 = new JPanel();
    _myPanel1.add(new JLabel("Panel 1"));
    _myPanel1.setVisible(true);

    _myPanel2 = new JPanel();
    _myPanel2.add(new JLabel("Panel 2"));
    _myPanel2.setVisible(false);

    JButton button = new JButton("Switch to Panel2");
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(_myPanel1.isVisible()) {
                _myPanel1.setVisible(false);
                _myPanel2.setVisible(true);
                button.setText("Switch to Panel1");
            } else {
                _myPanel1.setVisible(true);
                _myPanel2.setVisible(false);
                button.setText("Switch to Panel2");
            }

        }
    });

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.PAGE_START;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 1;
    gbc.weighty = 0;

    mainPanel.add(button,gbc);

    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridwidth = 2;
    gbc.gridheight = 2;
    gbc.weightx = 1;
    gbc.weighty = 1;
    mainPanel.add(_myPanel1,gbc);
    mainPanel.add(_myPanel2,gbc);

    frame.add(mainPanel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}
public static void main(String[] args) {
    PanelExample exmp = new PanelExample();
    exmp.init();
}

}

Soma
  • 24
  • 4
0

here a simple example that you can follow please show code at lest so we can follow your problem this a snap of code from unknowing source (old one)

    package layout;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class CardLayoutExample extends JFrame{
    JPanel totelPanel,btnPan,showPan;
    JButton btn1,btn2;

    public static void main(String[] args) {
        CardLayoutExample ex = new CardLayoutExample();
        ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ex.pack();
        ex.setLocationRelativeTo(null);
        ex.setTitle("BookClube library system");
        ex.setVisible(true);
    }

    public CardLayoutExample(){
        btn1 = new JButton("menu button");
        btn2 = new JButton("back button");
        CardLayout c1 = new CardLayout();

        btnPan = new JPanel();
        btnPan.add(btn1);

        showPan = new JPanel();
        showPan.add(btn2);

        totelPanel = new JPanel(c1);
        totelPanel.add(btn1,"1");
        totelPanel.add(btn2,"2");
        c1.show(totelPanel,"1");

        JPanel fullLayout = new JPanel(new BorderLayout());
        fullLayout.add(totelPanel,BorderLayout.NORTH);

        add(fullLayout);
        btn1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                c1.show(totelPanel,"2");
            }
        });

        btn2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                c1.show(totelPanel,"1");
            }
        });


    }

}
isslam akkilah
  • 418
  • 1
  • 11
  • 23