How to remove a button from a frame?

JFrame frame = new JFrame(); JButton button = new JButton(); frame.getContentPane().add(BorderLayout.SOUTH, button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); frame.setVizible(true); 

And now, how do I remove a button from a frame?

  • one
    frame.getContentPane().remove(button); - Senior Pomidor

2 answers 2

Use the remove method:

 frame.remove(button); 

or

 button.getParent().remove(button); 

    Use Remove :

     panel.remove(button);