October 27, 2012

how to get integer value from textfield and retrieve the value to another class in java

Question by user1778569

How to get integer value from textfield and retrieve the value to another class in java ??

I know how to get but if i used in another class he get me 0 value,,,

“this is in the GUI” x1,x2,y1,y2 predefined

private void X2_accesActionPerformed(java.awt.event.ActionEvent evt){              
     x2=(int)(Double.parseDouble(X2_acces.getText())); 
}

I want to draw line the user inter 2 point (x1,y1) and (x2,y2) after that move the value to class draw line

“this is in class drawLine”

Next_frame nf=new Next_frame();
Point2D.Double p2=new Point2D.Double(nf.x2,nf.y2);

Answer by Starx

The following statement is quite ambigous.

x2=(int)(Double.parseDouble(X2_acces.getText())); 

You are parsing the value as Double and casting it down to int. This is enough:

x2 = Integer.parseInt(x2_acces.getTest());

Now, when passing this value to another class, you have to make sure, that the method to which you are passing to accept the data type you are about to send.

For example:

For a class like this:

class DummyClass {
   public DummyClass(int X) {
      //Here note that 'x' is asking for a integer so you have to provide it with integer
   }
}

Then you have use them in the following order:

x2 = Integer.parseInt(x2_acces.getTest());
DummyClass c = new DummyClass(x2);

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!