java - components in JFrame does not appear when i create a table -


i new in java programming. going well. seems having problem in jframe whenever create jtable called in jscrollpane. components (e.i. buttons, labels etc.) won't appear. when comment table , properties, components appear. may problem? thank help. lot! here method creating table. again.

package myphonebookbeta;  import java.awt.event.*; import java.sql.*; import java.util.arraylist; import java.util.vector; import java.util.logging.level; import java.util.logging.logger;  import javax.swing.*; import javax.swing.table.defaulttablemodel;  public class mainform extends jframe implements actionlistener{  //<editor-fold defaultstate="collpased" desc="my components">  jframe f=new jframe(); jpanel p=new jpanel(null);  jlabel fnamelbl,lnamelbl,addresslbl,cplbl; jtextfield fname,lname,address,cp; jbutton save,delete,cancel,update; jtable table=null; jscrollpane scrollpane=null; jdesktoppane dp=null; //</editor-fold>  //<editor-fold defaultstate="collapsed" desc="sql variabled">  private final string url="jdbc:mysql://localhost:3306/"; private final string db="dbnetbeans"; private final string uid="root"; private final string pwd=""; private final string driver="com.mysql.jdbc.driver"; private connection con=null; private statement st=null; private resultset rs; private resultsetmetadata md; private string sql;  //</editor-fold>  public mainform(){     mainformgui();  }//end mainform  private void mainformgui(){     f.settitle("my phonebook beta");    f.setsize(600,400);    f.setlocationrelativeto(f);    f.setvisible(true);    f.setdefaultcloseoperation(do_nothing_on_close);    f.addwindowlistener(new windowadapter(){        @override        public void windowclosing(windowevent e){            int cls=joptionpane.showoptiondialog(null,"you exit program! wish proceed?","warning",                    joptionpane.yes_no_option,joptionpane.warning_message,null,null,null);            if (cls==0){                system.exit(0);            }        }    });      initcomponents();  }//end of mainformgui  private void initcomponents(){      //<editor-fold defaultstate="collapsed" desc="components declaration">      fnamelbl=new jlabel("first name : "); fnamelbl.setbounds(10,18,90,24);     lnamelbl=new jlabel("last name : "); lnamelbl.setbounds(10,52,90,24);     addresslbl=new jlabel("address : "); addresslbl.setbounds(10,86,90,24);     cplbl=new jlabel("contact no. :"); cplbl.setbounds(10,120,90,24);      fname=new jtextfield(""); fname.setbounds(100,14,140,24);     lname=new jtextfield(""); lname.setbounds(100,48,140,24);     address=new jtextfield(""); address.setbounds(100,82,140,24);     cp=new jtextfield(""); cp.setbounds(100,116,140,24);      save=new jbutton("save"); save.setbounds(10,150,110,24);     cancel=new jbutton("cancel"); cancel.setbounds(130,150,110,24);     update=new jbutton("update"); update.setbounds(10,180,110,24);     delete=new jbutton("delete"); delete.setbounds(130,180,110,24);       //</editor-fold>      //<editor-fold defaultstate="collapsed" desc="adding panel">     p.setvisible(true);      p.add(fnamelbl); p.add(fname);     p.add(lnamelbl); p.add(lname);     p.add(addresslbl); p.add(address);     p.add(cplbl); p.add(cp);      p.add(save);p.add(delete); p.add(update); p.add(cancel);       //</editor-fold>      btnaction(); 

// createtable();

    f.add(p);    }//end of initcomponents  private void btnaction(){     cancel.addactionlistener(this);     save.addactionlistener(this);     delete.addactionlistener(this);  }//end void btnaction  @override public void actionperformed(actionevent ae){      string fn=fname.gettext();     string ln=lname.gettext();     string ad=address.gettext();     string cpn=cp.gettext();      try{          //<editor-fold defaultstate="collapsed" desc="cancel_click">          if(ae.getsource()==cancel){              clearfields();          }//end cancel          //</editor-fold>          //<editor-fold defaultstate="collapsed" desc="save_click">          if(ae.getsource()==save){              sql="insert tbphonebook (firstname,lastname,address,cp)"                     + " value('"+fn+"','"+ln+"','"+ad+"','"+cpn+"')";             connect();             st=con.createstatement();             st.executeupdate(sql);             disconnect();             joptionpane.showmessagedialog(null,"info saved!","confirmation - save ", joptionpane.information_message);             clearfields();          }//end save          //</editor-fold>          //<editor-fold defaultstate="collapsed" desc="delete_click">          if(ae.getsource()==delete){              int ch=joptionpane.showoptiondialog(null,"this action irrevocable. wish tocontinue?",                     "delete contact?",joptionpane.yes_no_option,joptionpane.warning_message,null,null,null);              if (ch==0){                  sql="delete tbphonebook firstname '"+fn+"' , lastname '"+ln+"'";                 connect();                 st=con.createstatement();                 st.executeupdate(sql);                 disconnect();                 joptionpane.showmessagedialog(null,"contact deleted!","confirmation - delete ", joptionpane.information_message);                 clearfields();              }//end if(ch==0)          }//end delete          //</editor-fold>       }//try      catch(exception e){             e.printstacktrace();     }//catch }//end actionperformed  private void connect(){     try{        class.forname(driver).newinstance();        con=drivermanager.getconnection(url+db,uid,pwd);    }     catch(exception ex){        ex.printstacktrace();    }   }//end connect  private void disconnect(){     try{        con.close();    }     catch(exception ex){        ex.printstacktrace();    }   }//end disconnect     private void clearfields(){     fname.settext("");    lname.settext("");    address.settext("");    cp.settext("");    fname.requestfocus();  }//end clearfield  private void createtable(){      scrollpane=new jscrollpane(this.table);     scrollpane.setbounds(250,14,320,160);     scrollpane.setvisible(true);      scrollpane.setviewportview(table);     p.add(scrollpane);  }//end createtable  private void filltable() throws sqlexception{      arraylist colnames=new arraylist();     arraylist data=new arraylist();     arraylist row=new arraylist();      sql="select * tbphonebook";     connect();     st=con.createstatement();     rs = st.executequery(sql);      try{          md =rs.getmetadata();         int col=md.getcolumncount();          //to getcolumn names         (int i=1;1<=col;i++){             colnames.add(md.getcolumnname(i));         }          //to row data         while(rs.next()){              for(int i=1;1<=col;i++){                  row.add(rs.getobject(i));              }              data.add(row);         }      }     catch(exception ex){          ex.printstacktrace();      }   }//end filltable } 

jpanel p=new jpanel(null);  

do not use null layout unless have to. read this article on how use gridbaglayout. easy use , flexible.

about repaint problem: sure execute code on event dispatch thread?


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -