swing - Communication between GUI Java -
i have 3 classes create objects jpanels ie myobject extends jpanel. call 3 panels in main method call.
myobject1 mo1 = new myobject1(); // contains textfield , button myobject2 mo2 = new myobject2(); // contains textarea holding long paragraph myobject3 mo3 = new myobject3();
how can mo1 call methods on mo2 changing text of text area?
thanks suggestions guys! gonna go ahead , accept first answer. solved problem.
update 1 of these panels combination of 2 panels in program, hindering ability pass instance of object class. removing panel class , creating jpanel in main adding 2 panels new jpanel able pass instances of classes each constructor.
so in turn solution
jpanel panelholder = new jpanel(); // create panel in main instead of new class myobject2 mo2 = new myobject2(); // contains textarea holding long paragrah myobject1 mo1 = new myobject1(mo2); // contains textfield , button panelholder.add(mo1); panelholder.add(mo2); myobject3 mo3 = new myobject3();
and in mo2 class:
private myobject1 object1; // constructor mo2 class public myobject2(myobject1 object1){ this.object1 = object1; // instance of object1 ....// other constructor pieces }
again guys!
why don't pass reference myobject1
instance constructor of myobject2
?
myobject1 mo1 = new myobject1(); myobject2 mo2 = new myobject2(mo1);
then, (assuming method public or package-private) can call mo1.methodname()
mo2
. mentioned in comments, should instantiate components plan edit in mo1
in constructor, avoid nullpointerexceptions when make call mo2
.
Comments
Post a Comment