java - how to pass textfield from main class to another class -


how pass textfield main class pri countertask1 class.

below program example. real program contains similar construction.

in countertask1 class textfield add string.if click print button should print textfield in terminal.

advance thanks.

import java.io.*; import javax.swing.*;  import java.awt.event.*;   import javax.swing.swingworker; import java.util.list; public class pri  {  jframe frame1 = new jframe();  jlabel sourcelabel1    = new jlabel("source name");   jtextfield sourcefield1 = new jtextfield(20);  public void mainframe()          {   final countertask1 task1 = new countertask1();   frame1.setlayout(null);       jbutton print = new jbutton("print");   print.setbounds(10,10,100,30);   print.addactionlistener(new actionlistener()   { public void actionperformed(actionevent ae)     { string sourcename=sourcefield1.gettext();       system.out.println("printing in terminal "+sourcename);       task1.execute();          } });    jbutton exit =  new jbutton("exit");   exit.setbounds(10,50,100,30);   exit.addactionlistener(new actionlistener()   { public void actionperformed(actionevent ae)     { system.exit(0); } });           sourcefield1.setbounds(130,10,100,30);         frame1.add(sourcefield1);         frame1.add(print);         frame1.add(exit);         frame1.pack();          frame1.setsize(250,150);         frame1.setlocation(100,100);         frame1.setvisible(true);         frame1.setdefaultcloseoperation(jframe.exit_on_close);  } //mainframe  public static void main(string[] args) {   swingutilities.invokelater(new runnable()     { public void run()         {             pri frame = new pri();             frame.mainframe();  }   }); }   }   class countertask1 extends swingworker<integer, integer>   { protected integer doinbackground() throws exception  {          string 1 = sourcefield1.gettext();         string 2 = "thanks !";         string addst = 1 +two ;         system.out.println("printing in task" + addst);         return 0;    }// protected main class  protected void process(list<integer> chunks) {     system.out.println(chunks);   }  } // counter task 

i things differently -- pass text swingworker in constructor:

  print.addactionlistener(new actionlistener() {      public void actionperformed(actionevent ae) {         string sourcename = sourcefield1.gettext();         system.out.println("printing in terminal " + sourcename);          // note change in constructor         // way gettext() called on edt.         countertask1 task1 = new countertask1(sourcefield1.gettext());         task1.execute();      }   }); 

and in other class:

class countertask1 extends swingworker<integer, integer> {    private string text;     public countertask1(string text) {       this.text = text;    }     protected integer doinbackground() throws exception {        string 1 = text;       string 2 = "thanks !";       string addst = 1 + two;       system.out.println("printing in task" + addst);       return 0;    } 

note:

  • if need second class call methods of first, want pass reference of first second, similar how passing string above.
  • be sure not make swing calls doinbackground() method.
  • learn , adhere java naming convention can better understand future code posts. class names should start capital letter , field, variable , method names lower-case letter.

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -