java - get value jtextfield from another class to actionperformed method -


i have 2 class, 1 swing , main class, 1 actionperformed method

//class swing package system; import javax.swing.*; import java.awt.event.*;  public class gui{    jframe frame = new jframe("frame");    jtextfield tx = new jtextfield(10);    jbutton bt = new jbutton("button");     public void gettx(){       return tx.gettext();    }     public void init(){       frame.setsize(200, 200);       frame.add(tx);       frame.add(bt);       tx.setbounds(20, 20, 140, 50);       bt.setbounds(20, 100, 120, 40);       btaddcom.addactionlistener(new actionlistener(){         @override         public void actionperformed(actionevent e){             control.btclicked(e);         }     });    }     public gui(){       init();    }     public static void main(string[] args) {     java.awt.eventqueue.invokelater(new runnable(){         public void run() {             new gui().frame.setvisible(true);         }     });   } } 

below other class

package system; import java.awt; import java.awt.event.*;  public abstract control implements actionlistener{     public static void btclicked(actionevent e){          gui gui = new gui();          string txf = gui.gettx();          joptionpane.showmessagedialog(null, txf);     } } 

my question why cannot value jtextfield tx, because blank whenever text filled it. thanks

you're making new instance of gui class, fields empty/reset.

here's should do:

public abstract control implements actionlistener{     public static void btclicked(actionevent e, gui gui){         string txf = gui.gettx();         joptionpane.showmessagedialog(null, txf);     } } 

usage:

btaddcom.addactionlistener(new actionlistener(){     @override     public void actionperformed(actionevent e){         control.btclicked(e, gui.this);     } }); 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -