java - How to Auto Subtract input values of JText Field in swing -
this question has answer here:
i want auto subtract jtextinput
value , want subtracted value in text field
q. how can achieve this?
here jtextfield code:
public class showdata1 extends jframe { public showdata1() { this.setdefaultcloseoperation(jframe.dispose_on_close); container c = getcontentpane(); c.setlayout( new flowlayout()); // setundecorated(true); dimension screensize = toolkit.getdefaulttoolkit().getscreensize(); setbounds(0,0,screensize.width, screensize.height); opening_km= new jlabel("opening km"); opening_km.setbounds(0,280,258,30); opening_km.setfont(new java.awt.font("times new roman", 1,25)); // noi18n c.add(opening_km); opening_kms= new jtextfield(""); opening_kms.setbounds(200,280,258,30); opening_kms.setfont(new java.awt.font("times new roman", 1,25)); // noi18n c.add(opening_kms); closing_km= new jlabel("closing km"); closing_km.setbounds(500,280,258,30); closing_km.setfont(new java.awt.font("times new roman", 1,25)); // noi18n c.add(closing_km); closing_kms= new jtextfield(""); closing_kms.setbounds(700,280,258,30); closing_kms.setfont(new java.awt.font("times new roman", 1,25)); // noi18n c.add(closing_kms); totalkm= new jlabel("total km"); totalkm.setbounds(0,330,258,30); totalkm.setfont(new java.awt.font("times new roman", 1,25)); // noi18n c.add(totalkm); totalkms= new jtextfield(""); totalkms.setbounds(200,330,258,30); totalkms.setfont(new java.awt.font("times new roman", 1,25)); // noi18n c.add(totalkms); setlayout(null); } public static void main( string[] args ) { final showdata1 frame = new showdata1(); frame.setvisible(true); } }
try this..
int val = integer.parseint(textfield1.gettext()) - integer.parseint(textfield2.gettext());
for more please refer https://docs.oracle.com/javase/tutorial/java/data/converting.html
create class this..
public class mytextfield extends jtextfield { // whaterver want... public integer getvalue(){ if(gettext().length() == 0) return 0; return integer.parseint(gettext()); } }
then can int value call getvalue()
method
Comments
Post a Comment