Neha Mane
Neha Mane

Reputation: 21

How to add a new JPanel instance every time I click on JButton in JFrame in Java Swing

I am working with the Eclipse IDE. I have added Panel from a different class extending JPanel to a JFrame of a different class.

I have added a 'New' JButton on Frame. I added <JPanel instance>.setVisible(true) on actionListener event of that button. But when I press the 'New' button it shows the previous instance of JPanel. I want to add a new instance of that panel when I press a 'New' JButton.

Here is the code for my button:

final Rec r = new Rec();
JButton btnNew = new JButton("New Receipt");
btnNew.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        r.setVisible(true);
        contentPane.add(r,BorderLayout.CENTER);
    }
});

Here, Rec is a class extending JPanel.

Upvotes: 1

Views: 4089

Answers (2)

joey rohan
joey rohan

Reputation: 3566

I don't know your full code, so did a short exmaple here to clear up things.

  • As stated in above answer, you need to call revalidate() and repaint()
  • Change/modify as per your LayoutManager

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import javax.swing.SwingUtilities;
    
    /**
     * @author rohan
     */
    public class TestPanel {
    
        private JFrame frame;
        private JPanel panel;
        private JTextArea jTextArea;
        private JButton butt;
    
        TestPanel() {
            createGUI();
            process1();
        }
    
        private void process1() {
            jTextArea.setText("hii");
            frame.setVisible(true);
        }
    
        private void createGUI() {
            butt = new JButton("button");
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            panel = new JPanel();
            jTextArea = new JTextArea(20, 20);
            panel.add(jTextArea);
            panel.add(butt);
            butt.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    final JPanel pan = new JPanel();
                    JButton but = new JButton("CHANGED");
                    pan.add(but);
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            frame.add(pan);
                            butt.setVisible(false);
                            jTextArea.setVisible(false);
                            pan.revalidate();
                            pan.repaint();
                        }
                    });
                }
            });
            frame.add(panel);
            frame.pack();
            frame.setVisible(false);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new TestPanel();
                }
            });
        }
    }
    

Upvotes: 1

CodeBlind
CodeBlind

Reputation: 4569

First, you need to use a LayoutManager on your frame that supports multiple panels. I suggest look into FlowLayout for now - it's the simplest to learn if you're starting out fresh with Swing. You also need to call frame.add(panel) to display the panel. You shouldn't need to call setVisible(true) as all JPanel instances come visible by default.

You also need to call revalidate() and repaint() on your JFrame whenever you add or remove a panel - this forces the layout to update itself with any changes that were made in the JFrame's underlying Component list.

Upvotes: 2

Related Questions