java - Add Values from JTextFied to JTable -


    public class billdetailspanel implements actionlistener {           jpanel panel;         int flag = 0;         jlabel litemname, lprice, lqty, ltax, ldisprice;         jtextfield price, qty, tax, disprice;         jcombobox<string> itemname;         string[] booktitles = new string[] { "effective java", "head first java",                 "thinking in java", "java dummies" };          jbutton addbtn          public billdetailspanel() {             panel = new jpanel();             panel.setpreferredsize(new dimension(900, 50));             flowlayout layout = new flowlayout(flowlayout.center, 5, 15);             panel.setlayout(layout);             // panel.setbackground(color.green);              litemname = new jlabel("item name");             lprice = new jlabel("price");             lqty = new jlabel("quantity");             ltax = new jlabel("tax");             ldisprice = new jlabel("discount price");              itemname = new jcombobox<string>(booktitles);             itemname.addactionlistener(this);             price = new jtextfield(8);             // price.seteditable(false);             qty = new jtextfield(4);             tax = new jtextfield(5);             // tax.seteditable(false);             disprice = new jtextfield(8);              addbtn = new jbutton("add");             addbtn.addactionlistener(this);              panel.add(litemname);             panel.add(itemname);             panel.add(lprice);             panel.add(price);             panel.add(lqty);             panel.add(qty);             panel.add(ltax);             panel.add(tax);             panel.add(ldisprice);             panel.add(disprice);             panel.add(addbtn);             panel.setvisible(true);          }      public void actionperformed(actionevent e) {      billtablepanel btp=new billtablepanel();             string[] data=new string[5];               data[0]=(string) itemname.getselecteditem(); data[1]=price.gettext();     data[2]=qty.gettext();     data[3]=tax.gettext();     data[4]=qty.gettext();     btp.model.addrow(data);            btp.model.addrow(data);             system.out.println(data+"dataaaaaaaaaaaa");               }      }        }       public class billtablepanel implements actionlistener{         public jpanel panel;         public jtable table;         public jscrollpane scrollpane, scrollpane1;         public defaulttablemodel model;         public int a=10;     string[] data=new string[5];         public billtablepanel () {              panel = new jpanel();             panel.setlayout(null);              model = new defaulttablemodel();              string columnnames[] = { "item name", "actual price", "qty", "tax",                     "price" };              table = new jtable();             model.setcolumnidentifiers(columnnames);             table.setmodel(model);             table.setfocusable(false);             scrollpane = new jscrollpane(table);             scrollpane.setbounds(0, 0, 850, 100);              panel.add(scrollpane);         }      <br>       public class testclassframe {         jframe f;          billdetailspanel bill = new billdetailspanel();         billtablepanel billtablepanel = new billtablepanel();         public testclassframe() {              f = new jframe("zeon systems");             f.setlayout(null);              bill.panel.setbounds(0, 0, 900, 100);              f.add(bill.panel);             billtablepanel.panel.setbounds(0, 100, 900, 500);             f.add(billtablepanel.panel);             f.pack();             f.setsize(900, 550);             f.setvisible(true);             f.setdefaultcloseoperation(jframe.exit_on_close);          }           public static void main(string[] args) {          new testclassframe();          }  } 

problem code class bill detais contain text boxes , button billtablepane class contain jtable want add items billdetaisapanel jtable

on clicking jbutton not showing error values not inserting on it

the full source there please me find logical error,

in actionperformed method, you're creating new billtablepanel object, @ line (1), , trying add table model on line (2):

public void actionperformed(actionevent e) {      billtablepanel btp=new billtablepanel();  // **** (1)     // ...      btp.model.addrow(data);  // ***** (2) 

but understand that new billtablepanel that, new , distinct object, 1 unrelated 1 displayed. change state of displayed data, must call methods on displayed billtablepanel object, not on new 1 create actionperformed method.

for example, here's similar minimal program:

import java.awt.borderlayout; import java.awt.event.actionevent;  import javax.swing.*; import javax.swing.table.defaulttablemodel;  public class tableexample extends jpanel {     private holdstable holdstable = new holdstable();     private jtextfield lastnamefield = new jtextfield(10);     private jtextfield firstnamefield = new jtextfield(10);       public tableexample() {         jpanel fieldpanel = new jpanel();         fieldpanel.add(new jlabel("last name:"));         fieldpanel.add(lastnamefield);         fieldpanel.add(new jlabel("first name:"));         fieldpanel.add(firstnamefield);          jpanel buttonpanel = new jpanel();         buttonpanel.add(new jbutton(new abstractaction("your action") {              @override             public void actionperformed(actionevent evt) {                 holdstable ht = new holdstable(); // creates new reference -- bad!                 string lastname = lastnamefield.gettext();                 string firstname = firstnamefield.gettext();                  ht.addname(lastname, firstname);             }         }));         buttonpanel.add(new jbutton(new abstractaction("my action") {              @override             public void actionperformed(actionevent evt) {                 // holdstable ht = new holdstable();                 string lastname = lastnamefield.gettext();                 string firstname = firstnamefield.gettext();                  // ht.addname(lastname, firstname);                 holdstable.addname(lastname, firstname); // use ref displayed object             }         }));          setlayout(new borderlayout());         add(holdstable, borderlayout.center);         add(fieldpanel, borderlayout.page_start);         add(buttonpanel, borderlayout.page_end);     }      private static void createandshowgui() {         tableexample mainpanel = new tableexample();          jframe frame = new jframe("tableexample");         frame.setdefaultcloseoperation(jframe.dispose_on_close);         frame.getcontentpane().add(mainpanel);         frame.pack();         frame.setlocationbyplatform(true);         frame.setvisible(true);     }      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             public void run() {                 createandshowgui();             }         });     } }  class holdstable extends jpanel {     private static final string[] col_names = { "last name", "first name" };     private defaulttablemodel model = new defaulttablemodel(col_names, 0);     private jtable table = new jtable(model);      public holdstable() {         setlayout(new borderlayout());         add(new jscrollpane(table));     }      public void addname(string lastname, string firstname) {         string[] row = { lastname, firstname };         model.addrow(row);     } } 

your program creates new non-displayed object, , changes properties, similar code in program above:

        @override         public void actionperformed(actionevent evt) {             holdstable ht = new holdstable(); // creates new reference --                                               // bad!             string lastname = lastnamefield.gettext();             string firstname = firstnamefield.gettext();              ht.addname(lastname, firstname);         }     })); 

but since object state being changed, here ht, in code btp, not 1 displayed, nothing show.

the correct way shown in other action:

        @override         public void actionperformed(actionevent evt) {             // holdstable ht = new holdstable();             string lastname = lastnamefield.gettext();             string firstname = firstnamefield.gettext();              // ht.addname(lastname, firstname);             holdstable.addname(lastname, firstname); // use ref                                                      // displayed object         } 

i create field of gui view holds jtable, here holdstable , call method on it. since holdstable visible, changes in state shown in program (here jtable).


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -