java - how to change setIndeterminate jProgressbar in other frame -
i have 2 frame, framea , frameb, in framea add 1 button name buttona show frameb , progressbar (setindeterminate(false)), in frameb add 1 button name buttonb , want when click buttonb, progressbar in framea.setindeterminate(true)
in framea
frameb b; public framea() { initcomponents(); progressbar.setindeterminate(false); b = new frameb(); } public jprogressbar getprogressbar() { return progressbar; } private void buttonaactionperformed(java.awt.event.actionevent evt) { b.setvisible(true); }
in frameb use code in event buttonb clicked
private void buttonbactionperformed(java.awt.event.actionevent evt) { framea a= new framea(); a.getprogressbar().setindeterminate(true); }
but doesnt worked
this...
private void buttonbactionperformed(java.awt.event.actionevent evt) { framea a= new framea(); a.getprogressbar().setindeterminate(true); }
isn't going work, you've created instance of framea
that's not visible. has no relationship frame open.
there number of ways achieve this...
you could...
pass reference of framea
frameb
part of constructor call frameb
. in actionperformed
method, use reference change progress bar state.
but create tight coupling between framea
, frameb
reduce re-use of frameb
you could...
provide means interested party attach actionlistener
frameb
triggered when button clicked.
this assumes work flow , exposes components outside sources (via actionevent#getsource
), allow people change component...
you should...
fire propertychanged
event.
this easiest , safest of options i've come with. using property change listener in manner means don't need expose jprogressbar
, jbutton
or produce tight link between 2 frames.
property change support built in container
components/controls have it.
for example, attach propertychangelistener
b
when construct it.
b = new frameb(); b.addpropertychangelistener(new propertychangelistener() { @override public void propertychange(propertychangeevent evt) { if (evt.getpropertyname().equals("progressstate")) { progressbar.setindeterminate((boolean)evt.getnewvalue()); } } });
add in bframe
's actionperformed
method, simple call...
firepropertychange("progressstate", false, true);
when want set indeterminate state (you swap boolean
values reset if wanted to)
Comments
Post a Comment