java - Can a frame access buttons/componenets of another frame? -
i have 2 frames , want click button in frame2 , disable button in frame1. possible? program begins execution frame1 , opens frame2. it's frame2 want disable button in frame1 not work.....how can done?
additional information: have similar problems when work panels well. don't it. plz help!
here coding frame1 program begins execution:
public class frame1 extends javax.swing.jframe { frame2 frm2 = new frame2(); public frame1() { initcomponents(); } public void buttondisable(){ btn1.setenabled(false); } private void btn1actionperformed(java.awt.event.actionevent evt) { frm2.setvisible(true); } public static void main(string args[]) { java.awt.eventqueue.invokelater(new runnable() { @override public void run() { new frame1().setvisible(true); } }); } public javax.swing.jbutton btn1; }
here coding frame2 want disable button from:
public class frame2 extends javax.swing.jframe { public frame2() { initcomponents(); } private void btn2actionperformed(java.awt.event.actionevent evt) { frame1 frm1 = new frame1(); frm1.buttondisable(); } public static void main(string args[]) { java.awt.eventqueue.invokelater(new runnable() { @override public void run() { new frame2().setvisible(true); } }); } public javax.swing.jbutton btn2; }
it's quite simple , abstraction work fine. here's basic way achieve want:
class frametwo extends jframe { private framone firstframe; public frametwo(frameone firstframe) { this.firstframe = firstframe; }; public void dosomething() { system.out.println(this.firstframe.somemethod()); }; };
basically passing instance of frameone
class frametwo
constructor. there few options achieve this, using static members, using both frames instances of class, using view
abstraction, etc. question simple oop, perhaps read more on that.
also, note can add import javax.swing.frame
top of file , can type class frameone extends frame
instead of class frameone extends javax.swing.frame
;
most important
it extremely uncommon , discouraged use multiple frames same application. consider creating views use jpanel
nest elements , can switch panels , dialogs inside same frame.
Comments
Post a Comment