java - How to pass variable data from a JFrame to another? -
let have textfield , button in frame 1, , after user clicked button, proceed next frame , display out of user entered in frame 1. have search web , non of worked me.
try following:
frame1.java public class frame1 extends jframe{ protected static frame2 frame2; protected jtextfield textfield = new jtextfield(); protected jbutton button = new jbutton("button"); public frame1() { jframe frame1 = new jframe(); frame1.setbounds(0, 0, 200, 100); frame1.setvisible(true); frame1.add(textfield, borderlayout.north); frame1.add(button, borderlayout.center); button.addactionlistener( newbuttonlistener( textfield,frame2.textfield)); } public static void main(string[] args) { frame2 = new frame2(); new frame1(); } } frame2.java public class frame2 { protected jtextfield textfield = new jtextfield(); public frame2() { jframe frame1 = new jframe(); frame1.setbounds(200, 0, 200, 100); frame1.setvisible(true); frame1.add(textfield, borderlayout.center); } } buttonlistener.java public class buttonlistener implements actionlistener{ protected jtextfield textfield; protected jtextfield textfield2; public buttonlistener(jtextfield textfield, jtextfield textfield2) { this.textfield = textfield; this.textfield2 = textfield2; } @override public void actionperformed(actionevent e) { if (e.getactioncommand().equals("button")) { system.out.println(textfield.gettext()); textfield2.settext(textfield.gettext()); } } }
Comments
Post a Comment