How to replace JPanel with another JPanel
Asked Answered
H

6

9

I want to replace a Jpanel with another one in a JFrame I already search and try my code but nothing's happen this is my code :

public class Frame extends JFrame {

    private Container contain;
    private JPanel reChange,reChange2;
    private JButton reChangeButton;

    public Frame() {
        super("Change a panel");
        setSize(350, 350);
        setLayout(null);
        setLocationRelativeTo(null);
        setResizable(false);

        reChange = new JPanel(null);
        reChange.setBackground(Color.red);
        reChange.setSize(240, 225);
        reChange.setBounds(50, 50, 240, 225);
        add(reChange);

        reChangeButton = new JButton("Change It");
        reChangeButton.setBounds(20, 20, 100, 20);
        add(reChangeButton);

        reChangeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //System.out.println("in");
                contain = getContentPane();
                contain.removeAll();
                //System.out.println("in2");

                reChange2 = new JPanel(null);
                reChange2.setBackground(Color.white);
                reChange2.setSize(240, 225);
                reChange2.setBounds(50, 50, 240, 225);
                //System.out.println("in3");

                contain.add(reChange2);
                validate();
                //System.out.println("in4");
                setVisible(true);
                //System.out.println("in5");
            }
        });

    }

    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

can someone help me ? Thanks a lot

Hubbell answered 14/2, 2013 at 12:12 Comment(0)
M
6
  1. don't to use AbsoluteLayout

  2. change validate(); in actionPerformed to contain.validate(); and follows with contain.repaint();

  3. rename class name (reserved Java word, or methods name) Frame (java.awt.Frame) to MyFrame (for example)

  4. use CardLayout instead of remove and then add a new JPanel on runtime

Modred answered 14/2, 2013 at 12:20 Comment(4)
+1 for cardlayout and advices, though I would recommend revalidate() over validate(). @ArdyYonathan See here for an example of CardLayoutJebel
@David Kroukamp not valid for all Java users, most of them still to use Java6 and minor version too (missed there most of restrictions for Windows OS)Modred
+1 true .. Lol but maybe our code will force them to get java 7 (or the latest version) :)Jebel
aaaach :-) this is mistake from helicopers admin users access in Windows, for example simple user (windows built_in access) never will be JRE updated, etc (most of updates, sure can be quite easy configurable, but why bothering with that, dot) ....Modred
C
3

Try this, Following code will load the second jPanel (NewOrder) to first jPanel(jpMain)

NewOrder no = new NewOrder(); //This is the object of Second JPanel

// jpMain - This is the First JPanel
jpMain.setLayout(new java.awt.BorderLayout());
jpMain.removeAll();
jpMain.add(no);
jpMain.revalidate();
Cyprinodont answered 19/3, 2019 at 9:46 Comment(1)
revalidate() is the clue.Faucher
M
2

You must call validate() and then repaint() on the containing panel after you do the remove and add operations.

contain.validate();
contain.repaint();
Motorcade answered 14/2, 2013 at 12:16 Comment(0)
J
1

you need to do like this :

     public void actionPerformed(ActionEvent e) {
        //System.out.println("in");
        contain = getContentPane();
        contain.removeAll();
        //System.out.println("in2");

        reChange2 = new JPanel(null);
        reChange2.setBackground(Color.white);
        reChange2.setSize(240, 225);
        reChange2.setBounds(50, 50, 240, 225);
        //System.out.println("in3");

        contain.add(reChange2);
        validate();
        repaint();
        //System.out.println("in4");
        setVisible(true);
        //System.out.println("in5");
    }
});
Juncaceous answered 14/2, 2013 at 12:20 Comment(0)
W
1

Several issues with your code. Here is fixed version:

public class Frame extends JFrame {

    private Container contain;
    private JPanel reChange,reChange2;
    private JButton reChangeButton;

    public Frame() {
        super("Change a panel");
        setSize(350, 350);
        getContentPane().setLayout(null); // Changed here
        setLocationRelativeTo(null);
        setResizable(false);

        reChange = new JPanel(null);
        reChange.setBackground(Color.red);
        reChange.setSize(240, 225);
        reChange.setBounds(50, 50, 240, 225);
        getContentPane().add(reChange); // Changed here

        reChangeButton = new JButton("Change It");
        reChangeButton.setBounds(20, 20, 100, 20);
        getContentPane().add(reChangeButton); // Changed here

        reChangeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                contain = getContentPane();
                contain.removeAll();

                reChange2 = new JPanel(null);
                reChange2.setBackground(Color.white);
                reChange2.setSize(240, 225);
                reChange2.setBounds(50, 50, 240, 225);

                contain.add(reChange2);
                invalidate(); // Changed here
                repaint(); // Changed here
            }
        });
    }

    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
Wayne answered 14/2, 2013 at 12:32 Comment(0)
K
1

Assume you have a function, generatePanel() that returns a JPanel that you save in an instance variable of type JPanel:

private JPanel panelWithDynamicContent;

Assume you have already placed that JPanel into a container, perhaps at a specific index within that other container and want to preserve the order of the components within that container. Instead of destroying everything using removeAll(), I prefer a more precise approach that replaces only the component that needs to be replaced:

private void replacePanel(){
   Container parent = this.panelWithDynamicContent.getParent();
   int index = parent.getComponentZOrder(this.panelWithDynamicContent);
   // remove the old edition of the panel
   parent.remove(this.panelWithDynamicContent);
   // generate the replacement panel
   this.panelWithDynamicContent = generatePanel();
   // place the replacement panel in the same relative location as the one it is replacing
   parent.add(this.panelWithDynamicContent, index);

   // must call both of these, in the correct order
   parent.validate();
   parent.repaint();
}
Kitts answered 24/4, 2018 at 22:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.