I was experimenting with java and I was trying to create my "JOptionPane". Basically it was a JPanel which returns a value. (JPanel has owner)
I wanted to make it behave like JOptionPane, it should destroy itself when the value was returned.
The method was:
public Value getValue(){
try{
dispose();
}finally{
return value;
}
}
I used a try - finally block to make sure that it will return the value, but with some more "experiments" code works without try - finally block.
How it is possible statements and methods working after dispose()?
Even this code is valid
public Person getPerson(){
dispose();
System.out.println("aaa");
printFromMethod();
return person;
}
And will the panel disposed after the return of the value?
As per java documentation, Dispose method releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children.
This does not imply the object can do it's functions as this method only cleans up screen resources and it does not affect your code which has nothing to do with UI.
However you should not call the Dispose method from JPanel in this case because it's still attached to it's parent so calling the method will not achieve what you want, rather what you should do is call the Dispose method in the JFrame