java - My JTable doesn't show up -
i'm new jtable, or gui matter, had been given assignment of building receipt program inside gui. manage basic things work, table looks awful. need on how display table properly
public static void main (string[] args) { arraylist <item> lol= new arraylist <item>(); item ayam = new item("ayam",5678); item kambing= new item("kambing",5014); item buaya= new item("buaya",3000); item bocoranquiz= new item("bocoranquiz",5000); lol.add(ayam); lol.add(kambing); lol.add(buaya); lol.add(bocoranquiz); jframe frame= new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new gridlayout(3, 0)); frame.setsize(1000, 1000); simpledateformat sdf = new simpledateformat("dd/m/yyyy"); string date = sdf.format(new date()); jcombobox combo1= new jcombobox(); combo1.setpreferredsize(new dimension(10, 10)); combo1.additem(ayam.getname()); combo1.additem(kambing.getname()); combo1.additem(buaya.getname()); combo1.additem(bocoranquiz.getname()); jlabel label1= new jlabel("invoice no: "); jlabel label2= new jlabel("invoice date : " + date); jlabel label3= new jlabel("item name " ); jlabel label4= new jlabel("item price "); jlabel label5= new jlabel("item quantity : "); jpanel panel1= new jpanel (); panel1.setlayout(new gridlayout(3,0)); panel1.add(label1); panel1.add(label2); class inputlistener implements actionlistener { public void actionperformed(actionevent event) { jcombobox<string> combo= (jcombobox<string>) event.getsource(); selected= (string) combo.getselecteditem(); alpha=10; if (selected.equals("ayam")) { alpha=5678; } else if (selected.equals("kambing")) { alpha=5014; } else if (selected.equals("buaya")) { alpha=3000; } else if (selected.equals("bocoranquiz")) { alpha=5000; } label3.settext("item name " + selected); label4.settext("item quantity " + alpha); } } actionlistener inputact = new inputlistener(); combo1.addactionlistener(inputact); panel1.add(combo1); jtextfield tf = new jtextfield(); jbutton adda = new jbutton("add"); string[] columnnames= {"name","price","quantity","total"}; defaulttablemodel tablemodel= new defaulttablemodel(columnnames,0); jtable table = new jtable(tablemodel); jscrollpane scrollpanel = new jscrollpane(table); class inputlistener2 implements actionlistener { public void actionperformed(actionevent event) { quantity= integer.parseint(tf.gettext()); double total= alpha * quantity; object[] data= {selected,alpha,quantity,total}; tablemodel.addrow(data); } actionlistener inputact2 = new inputlistener2(); adda.addactionlistener(inputact2); jpanel panel2= new jpanel(); panel2.setlayout(new gridlayout(10,0)); panel2.add(label3); panel2.add(label4); panel2.add(label5); panel2.add(tf); panel2.add(adda); panel2.add(scrollpanel); frame.add(panel1); frame.add(panel2); frame.setvisible(true); }
your code bit of mess, but, basic problem setting gridlayout
many rows, example
frame.setlayout(new gridlayout(3, 0));
should
frame.setlayout(new gridlayout(2, 0));
since you're adding 2 components frame
equally,
panel2.setlayout(new gridlayout(10, 0));
should
panel2.setlayout(new gridlayout(6, 0));
remember, gridlayout
divide container equal sections, if there nothing contained within row/column.
you may want consider using different layout manager, gridbaglayout
, or combination of layouts depending on basic needs.
see how use gridbaglayout more details.
Comments
Post a Comment