user3053928
user3053928

Reputation: 11

Java: geting return value from other class

i just want to get the value entered in my txtfield from other class

public class MyCostumizedDialog{
int x = 0 ;

public void showFrameDialog(){
   // Here are my components...
    txt1 = new Jtextfields;...//my jtxtfield
    .......
    btn1.addactionlister(....){
         x = Integer.parseInt(txt1.gettext());//get string from jtxtfld and parse to int
   }
public int getNumber(){
    return x;
}

}

then i want to get the value entered from jtxtfild from MyCostumizedDialog like this

public class OtherClass{

    public void frame(){

btn2.addactionlistener(......){
      MyCostumizedDialog mcd = new MyCostumizedDialog();
      mcd .showFrameDialog();
      Double x= mcd.getNumber();
      txtNumber.setText("P "+x);
     }
    }
}

txtnumber always show the initial value of x from MycostumeDialog,please help me

Upvotes: 0

Views: 117

Answers (2)

Ceiling Gecko
Ceiling Gecko

Reputation: 3186

Since you always instantiate a new instance of MyCostumizedDialog every time btn2 is pressed you get the initial value of MyCostumizedDialog because nobody pressed the btn1 for the newly created instance that would set the value.

If the btn1 needs to be there for other purposes I would recommend to just add another method to do exactly the same thing as the btn1 action listener does, and then call that method inside the btn2 action listener.

Upvotes: 0

Vilen
Vilen

Reputation: 5061

you are trying to get value before setting value to it in other words your x gets value once you click on btn1, but you are trying to get value of x before clicking on that button.

mcd .showFrameDialog();
Double x= mcd.getNumber();

you should call mcd.getNumber(); after you set value to your variable.

Upvotes: 1

Related Questions