Search code examples
javaswingjdialogjcomponent

Remove JPanel with some JComponents from JDialog


if created a JPanel and then added some JComponents with these rules

public class MyPanel extends JPanel {
    myButton = new MyButton()
    myButton.addXxxListener(...)
    myButton ....
    add(myButton)
}

public class MyButton extends JButton {
    //some constructors for parametrize JButton Object
    //build only JButton Object value or listener(s) are added in parent 
}
//and on runtime are all values added by using constructor or getter/setter 

then if I want to remove correctly this JPanel(s) from JDialog (remove all referencies and dispose that to the GC) I must

1/ at 1st. have to remove JComponents from JPanel, thenafter JPanel from JDialog

2/ just directly remove JPanel from JDialog


Solution

  • just removing the panel and all listeners should suffice, as long you don't reference any of its subcomponents from other objects.