java - Using a JDialog as input -



java - Using a JDialog as input -

i trying utilize jdialog input string. text before click button.

this dialog:

public class addmemberdialog extends jdialog { private jtextfield name; public addmemberdialog() { super(new jframe("add member"), "add member"); this.setdefaultcloseoperation(jdialog.hide_on_close); this.setminimumsize(new dimension(500, 500)); this.name = new jtextfield(); jbutton add together = new jbutton("add"); add.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { close(); } }); this.setlayout(new gridlayout(2, 1, 5, 5)); this.add(name); this.add(add); this.pack(); } private void close(){ this.dispose(); } public string getname(){ homecoming this.name.gettext(); } }

and here using string:

addmemberdialog input = new addmemberdialog(); input.setlocationrelativeto(this); input.setvisible(true); string txt = input.getname();

import javax.swing.jdialog; import javax.swing.jtextfield; import javax.swing.jbutton; import javax.swing.jframe; import java.awt.dimension; import java.awt.event.actionlistener; import java.awt.event.actionevent; import java.awt.gridlayout; public class addmemberdialog extends jdialog { private jtextfield name; public static void main(string[] args) { addmemberdialog input = new addmemberdialog(); input.setlocationrelativeto(null); input.setvisible(true); } public addmemberdialog() { super(new jframe("add member"), "add member"); this.setdefaultcloseoperation(jdialog.hide_on_close); this.setminimumsize(new dimension(500, 500)); this.name = new jtextfield(); jbutton add together = new jbutton("add"); add.addactionlistener( new actionlistener() { public void actionperformed(actionevent arg0) { close(); } }); jbutton takeinput = new jbutton("takeinput"); takeinput.addactionlistener( new actionlistener() { public void actionperformed(actionevent e) { string txt = getname(); system.out.println(txt); } } ); this.setlayout(new gridlayout(3, 1, 5, 5)); this.add(name); this.add(add); this.add(takeinput); this.pack(); } private void close() { this.dispose(); } public string getname() { homecoming this.name.gettext(); } }

alright, basically, issue here that, if leave code

addmemberdialog input = new addmemberdialog(); input.setlocationrelativeto(this); input.setvisible(true); string txt = input.getname();

it automatically take in input sec ide reaches line of code. namely, unless set before ide gets there (and ide gets there in milliseconds), won't take anymore input after that. compensate, don't allow programme take input until , ready, hence need button. in above code, made new jbutton , called takeinput. gave actionlistener , in actionlistener, made asked for. now, can command when input happens.

java swing jdialog

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -