java - Call to jFrame from jInternalFrame does not work -
i have main jframe homepage.java
, many other classes extend javax.swing.jinternalframe
. 1 of classes (jinternalframe) login.java
. check whether password correct in login.java
, if password correct, load success.java
. problem arises when have load success.java
page.
have function in homepage.java
purpose remove internal frames , load success.java class. function follows :-
public void login(){ jdesktoppane1.removeall(); system.out.println("pos 2"); success frame = new success(); frame.setvisible(true); jdesktoppane1.add(frame); setcontentpane(jdesktoppane1); try { frame.setselected(true); } catch (java.beans.propertyvetoexception e) { } }
i call function in login.java page follows :-
system.out.println("pos 1 "); (new homepage()).login();
the logic not work. but, receive output
pos 1 pos 2
which shows program flow correct. not error also. weird factor when calthe same login()
function menu in jframe itself, desired output. please solve dilemma. surely appreciated!
this code:
system.out.println("pos 1 "); (new homepage()).login();
creates new homepage object , calls method on it. oh calling correct method, , it's going homepage instance, it's not correct homepage instance, not 1 that's displayed, because you've created new one.
short term solution: valid reference displayed homepage instance , call method on it. passing class needs constructor or method parameter, or use swingutilities.getwindowancestor(...)
.
i.e.,
window window = swingutilities.getwindowancestor(login.this); if (window instanceof homepage) { homepage validhomepageref = (homepage) window; validhomepageref.login(); }
long term solution: don't create spaghetti code gui objects changing behaviors of other gui objects, instead refactor code la mvc or 1 of variants, gui can change model , other gui objects can notified of these changes via listeners , respond if want.
Comments
Post a Comment