database - How to display data in AbstractTableModel using java? -
i have created jframe , want create abstracttablemodel display data database using dbquery in grey box.
this first time doing , have been struggling long, hope receive help! thank you.
jbutton btnsubmit = new jbutton("submit"); btnsubmit.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { boolean conditionok = false; if(chckbxr.isselected()==false && chckbxr_1.isselected()==false && chckbxr_2.isselected()==false && chckbxr_3.isselected()==false){ joptionpane.showmessagedialog(frame,"please select client account"); } else if(fromdate.gettext().equals("")){ joptionpane.showmessagedialog(frame, "please enter starting date"); } else if(todate.gettext().equals("")){ joptionpane.showmessagedialog(frame, "please enter end date"); } else if(integer.parseint(fromdate.gettext()) > integer.parseint(todate.gettext())){ joptionpane.showmessagedialog(frame, "end date earlier starting date"); } else conditionok = true; if(conditionok==true){ int fromyear = integer.parseint(fromdate.gettext().substring(0, 4)); int frommonth = integer.parseint(fromdate.gettext().substring(4,6))-1; int fromday = integer.parseint(fromdate.gettext().substring(6,8)); int toyear = integer.parseint(todate.gettext().substring(0, 4)); int tomonth = integer.parseint(todate.gettext().substring(4,6))-1; int today = integer.parseint(todate.gettext().substring(6,8)); simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); simpledateformat sdf1 = new simpledateformat("yyyymmdd"); simpledateformat sdf2 = new simpledateformat("dd mmm yyyy"); dateformat formatter; formatter = new simpledateformat("yyyy-mm-dd"); calendar convertedtodate = calendar.getinstance(); convertedtodate.set(toyear,tomonth,today); calendar convertedfromdate =calendar.getinstance(); convertedfromdate.set(fromyear,frommonth,fromday); int monthdifferencecount = 1; //calculate months difference ( monthdifferencecount=1; convertedfromdate.compareto(convertedtodate) <0; monthdifferencecount++) { convertedfromdate.add(calendar.month, 1); convertedfromdate.set(convertedfromdate.day_of_month,convertedfromdate.getactualmaximum(calendar.day_of_month)); } convertedtodate.set(toyear,tomonth,today); convertedfromdate.set(fromyear,frommonth,fromday); //datearray , predate array used store date date format. shall use tradedate , setldate date predatearray[] = new date[monthdifferencecount]; date datearray [] = new date[monthdifferencecount]; (int i=0; <monthdifferencecount; i++) { convertedfromdate.add(calendar.month,-1); convertedfromdate.set(convertedfromdate.day_of_month,convertedfromdate.getactualmaximum(calendar.day_of_month)); predatearray[i] = convertedfromdate.gettime(); try { predatearray[i] = (date)formatter.parse(sdf.format(predatearray[i])); } catch (parseexception e1) { e1.printstacktrace(); } convertedfromdate.add(calendar.month, 1); convertedfromdate.set(convertedfromdate.day_of_month,convertedfromdate.getactualmaximum(calendar.day_of_month)); datearray[i] = convertedfromdate.gettime(); try { datearray[i] = (date)formatter.parse(sdf.format(datearray[i])); } catch (parseexception e1) { e1.printstacktrace(); } convertedfromdate.add(calendar.month, 1); } summaryfromdate = sdf2.format(datearray[0]); summarytodate = sdf2.format(datearray[datearray.length-1]); string datelist[] = new string[monthdifferencecount]; string predatelist[] = new string[monthdifferencecount]; (int i=0; i<datearray.length; i++) { datelist[i] = sdf1.format(datearray[i]); predatelist[i] = sdf1.format(predatearray[i]); } arraylist<string> cltacclist = new arraylist<string>(); if(chckbxr.isselected()==true){ cltacclist.add("10190r"); } if(chckbxr_1.isselected()==true){ cltacclist.add("10230r"); } if(chckbxr_2.isselected()==true){ cltacclist.add("10280r"); } if(chckbxr_3.isselected()==true){ cltacclist.add("10290r"); } } } }); btnsubmit.setbounds(37, 643, 89, 23); add(btnsubmit);
after submitting date abstracttablemodel should display data database.
table model:
public class mtablemodel { public static class mymodel extends abstracttablemodel { private list<object[]> data; private list<string> columnnames; public mymodel(list<string> columnnames, list<object[]> data) { super(); this.columnnames = columnnames; this.data = data; } @override public int getrowcount() { return data.size(); } @override public int getcolumncount() { return columnnames.size(); } @override public string getcolumnname(int column) { return columnnames.get(column); } @override public object getvalueat(int rowindex, int columnindex) { return data.get(rowindex)[columnindex]; } } protected void initui() { jframe frame = new jframe(mtablemodel.class.getsimplename()); list<string> columns = arrays.aslist("name", "gender"); list<object[]> data = new arraylist<object[]>(); (int = 0; < 50; i++) { object[] value = new object[2]; data.add(value); } jtable table = new jtable(new mymodel(columns, data)); frame.add(new jscrollpane(table)); frame.pack(); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { new mtablemodel().initui(); } }); }
}
your value
arrays getting initialized pair of null
entries default.
object[] value = new object[2];
try instead:
for (int = 0; < 50; i++) { object[] value = new object[]{"name" + i, math.random() < .5 ? "m" : "f"}; data.add(value); }
Comments
Post a Comment