java - Changing the value of a textfied in JFrame class1, JFrame class2,... by selecting an item from jtable in Jframe class3 -
i have 14 jframe classes , 1 jframe class holding jtable acts calendar. want date of text field or other component in of 14 jframe classes changed when user selects date jtable calendar . now, frames appear 1 @ time , jtable instantiated jbutton on of 14 jframe classes.
my dilemma how identify jframe class has instantiated jframe contains jtable it's text field value can changed.
code instantiates jframe containing table:
private void jbutton1actionperformed(java.awt.event.actionevent evt) { jframe frmmain = new calender(); frmmain.setvisible(true); frmmain.setsize(367, 342); } code changes value of textfield 1 of 14 jframe classes :
@override public void mouseclicked(mouseevent me) { if (me.getclickcount()==2){ int selected1 = jtable1.getselectionmodel().getleadselectionindex(); int selected2 =jtable1.getcolumnmodel().getselectionmodel().getleadselectionindex(); object c = jtable1.getmodel().getvalueat(selected1, selected2); sowinformation.jtextfield4.settext(c.tostring()); this.setvisible(false); } } where sowinformation 1 of 14 jframe classes.
as @andrewthompson wisely mentioned, the use of multiple jframe bad practice. in case (non-)modal dialog better choice.
now let's assume choose non-modal dialogs, still having same problem. there several ways solve need keep reference frame/dialog opened calendar's frame. better, can use interfaces define contract update text field isolating desired behavior actual implementation. example:
public interface iupdatetext { public void updatetext(string text); } now calendar's frame needs keep reference iupdatetext compliant object:
class calender extends jframe { ... private iupdatetext updatetext; ... public void setiupdatetext(iupdatetext ut) { this.updatetext = ut; } ... } then in mouse listener implementation call updatetext() method:
@override public void mouseclicked(mouseevent me) { if (me.getclickcount()==2){ int selectedrow = jtable1.getselectedrow(); int selectedcolumn = jtable1.getselectedcolumn(); if (selectedrow > -1 && selectedcolumn > -1) { object value = jtable1.getvalueat(selectedrow, selectedcolumn); // ask value view, not model. otherwise need convert both row , column indexes this.updatetext.updatetext(value.tostring()); this.setvisible(false); // maybe this.dispose() instead ? } } } of course other frames/dialogs can open calendar frame have implement iupdatetext interface. example sowinformation frame:
class sowinformation extends jframe implements iupdatetext { ... private jtextfield jtextfield4; ... @override public void updatetext(string text) { this.jtextfield4.settext(text); } ... } and set iupdatetext compliant object calendar frame when instantiate it:
private void jbutton1actionperformed(java.awt.event.actionevent evt) { calender frmmain = new calender(); calender.setiupdatetext(this); // assuming 'this' iupdatetext interface compliant frmmain.pack(); frmmain.setvisible(true); } other comments
from architectural point of view problem might better solved through mvc pattern, having controller acts intermediary both frames/dialogs. there multiple questions about mvc pattern , swing here in example/explanation shown in this answer @madprogrammer (a real swing guru).
Comments
Post a Comment