java - Default Button after dispose and setVisible -
given following code:
public class dialogtest implements actionlistener { public static void main(string[] args) {dialogtest g = new dialogtest();} public dialogtest() { jbutton b1 = new jbutton("button a"); b1.addactionlistener(this); jdialog d = new jdialog(); d.setdefaultcloseoperation(jdialog.dispose_on_close); jpanel p = new jpanel(); p.add(b1); d.add(p); d.getrootpane().setdefaultbutton(b1); d.pack(); d.setvisible(true); d.dispose(); d.pack(); d.setvisible(true); } public void actionperformed(actionevent e) {system.out.println("hello");} }
shouldn't pressing enter key write console? according docs (http://java.sun.com/javase/7/docs/api/java/awt/window.html#dispose()):
the window , subcomponents can made displayable again rebuilding native resources subsequent call pack or show. the states of recreated window , subcomponents identical states of these objects @ point window disposed
is intended behaviour?
the reason in jbutton.removenotify
(which seems called @ dispose
) defaultbutton
reset:
overrides
jcomponent.removenotify
check if button set default button onrootpane
, , if so, setsrootpane
's default button null ensurerootpane
doesn't hold onto invalid button reference.
public void removenotify() { jrootpane root = swingutilities.getrootpane(this); if (root != null && root.getdefaultbutton() == this) { root.setdefaultbutton(null); } super.removenotify(); }
Comments
Post a Comment