java - Reusing a panel to store values -


i need reuse textfield in panel store values.

refer long simple code.

basically doing

  1. creating panel containing textfield
  2. creating array of object of class holder has variable name going store values textfield

class holder {     string name; }   public class yummy12 extends holder {     int t;      public static void main(string args[])     {         new yummy12();     }      holder[] obj=new holder[5];      jbutton button1=new jbutton("add one");     jbutton button2=new jbutton("exit");     jpanel panel=new jpanel();     jtextfield textfield=new jtextfield("enter text here");     jlabel label1=new jlabel();     jframe frame=new jframe();      yummy12()     {         for(int i=0;i<5;i++)         {             obj[i]=new holder();         }          //set component bounds (only needed absolute positioning)         label1.setbounds (165, 75, 100, 25);         textfield.setbounds (350, 75, 100, 25);         button1.setbounds (170, 230, 100, 25);         button2.setbounds (360, 230, 100, 25);          panel.add(label1);         panel.add(textfield);         panel.add(button1);         panel.add(button2);          frame.add(panel);          frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setvisible(true);          frame.setextendedstate(jframe.maximized_both);          listenforbutton1 lrbutton1=new listenforbutton1();         listenforbutton1 lrbutton2=new listenforbutton1();          button1.addactionlistener((actionlistener)lrbutton1);         button2.addactionlistener((actionlistener)lrbutton2);     }              private class listenforbutton1 implements actionlistener     {            @override         public void actionperformed(actionevent e)         {             if(e.getsource()==button1)             {                 obj[t].name=textfield.gettext();                 t++;                  //what code should come here reuse same panel again???                   if(e.getsource()==button2)                 {                     system.exit(0);                 }             }         }     } 

your if block needs modified in order make work:

        if(e.getsource()==button1) {             obj[t].name=textfield.gettext();             t++;              //what code should come here reuse same panel again???              textfield.settext(""); // clears textfield re-use          } else if(e.getsource()==button2){                 system.exit(0);         } 

hope helps


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -