java - Click on JTable Model Updates JTextfield -
i have jtable custom abstract table model , select row in table , information appear in textboxes left. happen automatically out use use of buttons. approach can think of involves button. table model looks this:
public class admintablemodel extends abstracttablemodel { private arraylist<contestant> contestants; private string[] columns={"first name", "last name", "entry"}; public admintablemodel(arraylist<contestant> contestants) { this.contestants = contestants; } public admintablemodel(list l) { contestants.addall(l); } public int getrowcount() { return contestants.size(); } public int getcolumncount() { return columns.length; } public string getcolumnname(int col) { return columns[col]; } public object getvalueat(int row, int col) { contestant contestant = contestants.get(row); switch(col){ case 0: return contestant.getfirst_name(); case 1: return contestant.getlast_name(); case 2: return contestant.getentry(); default: return null; } } }
with code table looking this:
public class contestantstable extends jtable{ public contestantstable(arraylist<contestant> contestants) { admintablemodel atmodel = new admintablemodel(contestants); this.setmodel(atmodel); atmodel.firetabledatachanged(); } }
here panel text fields want refresh:
public class contestantaddpanel extends jpanel { private jlabel jlfirst_name; private jlabel jllast_name; private jlabel jlentry; private jtextfield jtffirst_name; private jtextfield jtflast_name; private jtextfield jtfentry; public contestantaddpanel() { jlfirst_name = new jlabel("first name: "); jllast_name = new jlabel("last name: "); jlentry = new jlabel("entry: "); jtffirst_name = new jtextfield(); jtflast_name = new jtextfield(); jtfentry = new jtextfield(); this.setlayout(new gridlayout(3,2)); this.add(jlfirst_name); this.add(jtffirst_name); this.add(jllast_name); this.add(jtflast_name); this.add(jlentry); this.add(jtfentry); } public string getfirstname(){ return jtffirst_name.gettext(); } public string getlastname(){ return jtflast_name.gettext(); } public string getentry(){ return jtfentry.gettext(); } }
i not looking write me solution, looking guidance cannot seem find want on google. such link tutorial, oracle 1 isn't informative on matter don't want update row in table in textfields.
jtable has listselectionmodel, can access getselectionmodel(). can add listener , notified of row selection events. listeners need implement javax.swing.event.listselectionlistener interface , notified via valuechanged(..). code something similar this:
public contestantaddpanel() implements listselectionlistener { ... public valuechanged(listselectionevent e) { ... } ... }
and
mytable.getselectionmodel().addlistselectionlistener(<some instance of panel>);
this should enough started, if need more info please post comment. don't have links tutorial, sorry.
edit:
Comments
Post a Comment