Skip to content
Advertisement

Update class variables on button click

If i click my “Run” button on my GUI after changing the value of a textfield, the variable won’t update.

I have the button in one class:

JavaScript

and in another class, i have all my Data collected:

JavaScript

in the RunSimulation class, the class variables in Data are accessed. If I print out Data.campusSize, it will always give the same value, even if I change it in the textField.

Advertisement

Answer

The problem with your current code is that your campusSize variable isn’t updated each time you change the value in the UI. The only time the getter for that UI component is called, is when you initialize your int.

A simple way to change your code:

JavaScript

Notice that now, I call a method, not an already instantiated variable.

JavaScript

This way, you’ll get the updated value of your textField each time you call that method, since you call the getter again.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement