netbeans - How do I change jButton text and save it when Java application is closed? -


how change jbutton text , save when java application closed?

i change name of jbutton @ runtime, save these new values future use, possible?

thank you

you try serialize button object after click , insert code (in main method) initialize button serialized file. modified code illustrate this. hope helps!

import javax.swing.*; import java.awt.*;  import java.io.*; import java.nio.file.*;  import java.awt.event.actionlistener; import java.awt.event.actionevent;  class buttonframe extends jframe  {     public static jbutton bchange ; //button appears in window     public static jtextfield btext ; //text field appears in window      // constructor buttonframe     buttonframe(string title, jbutton button)      {         super( title );                     // invoke jframe constructor         setlayout( new flowlayout() );      // set layout manager          //initialize textfield         btext = new jtextfield(40);         btext.settext("place new button name here , click button");         add(btext);          //initialize button in window text file         bchange = new jbutton(button.gettext());          add( bchange );                      //add event listener button change text of button in window         bchange.addactionlistener(new actionlistener() {              public void actionperformed(actionevent e)             {                 bchange.settext(btext.gettext());                   //save new button text file                 try {                     fileoutputstream fileout =                             new fileoutputstream("button.ser");                     objectoutputstream out = new objectoutputstream(fileout);                     out.writeobject(bchange);                     out.close();                     fileout.close();                 } catch(exception ex){                  }             }         });         setdefaultcloseoperation( jframe.exit_on_close );      } }  public class buttondemo  { public static jbutton jb = null;      public static void main ( string[] args )     {         try         {             //check serialized file exists             path path = paths.get("button.ser");              if (files.notexists(path)) {                 //create file                 fileoutputstream fileout =                     new fileoutputstream("button.ser");                 objectoutputstream out = new objectoutputstream(fileout);                 out.writeobject(new jbutton("click me!"));                 out.close();                 fileout.close();             }              //initailize button file              fileinputstream filein = new fileinputstream("button.ser");             objectinputstream in = new objectinputstream(filein);             jb = (jbutton) in.readobject();             in.close();             filein.close();         }catch(exception i)         {         }         buttonframe frm = new buttonframe("button demo", jb);          frm.setsize( 400, 200 );              frm.setvisible( true );        } } 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -