Passing dynamic variables during runtime
I have a program that has a counter in one class, which is incremented as a user clicks on a button. Then i have a label in another class and I'm attempting to pass the counter variable to the new class during runtime so that it updates as the user clicks the button. as of right now, the variable is passed, but it is static. it doesnt change when the user clicks the button. any suggestions?
the class that has the variable to be called:
Code:
public class Fetch extends Stage
//some other code
//....
public String toString() {
String temp;
if (myInstruction.flush == true) {
temp = "FLUSHED: \n" + myInstruction + "\n";
return temp;
}
if ((PC >= 0) && (PC < instructionSize) )
temp = Integer.toString(PC) + ":\n" + myInstruction + "\n";
else
temp = myInstruction + "\n";
return temp;
}
the class that is trying to get "temp" during runtime
Code:
public class DPanel extends JPanel
{
private JLabel PCCount;
public Fetch FetchCall = new Fetch();
DPanel()
{
String PCString = FetchCall.toString();
PCCount = new JLabel();
PCCount.setText(PCString);
}
}
Re: Passing dynamic variables during runtime
Whether or not a variable is static does not affect whether it can change. And you change the text of a JLabel is by calling the setText() method.
Re: Passing dynamic variables during runtime
hey Kevin, thanks for the response. i used the setText method to change the text by getting the return value "temp" from "public String toString()", temp changes during runtime. however, when i use the setText method, the JLabel doesnt update with the new value, it keeps the value that is stored when the program is first run. any ides?
Re: Passing dynamic variables during runtime
You have to call setText() whenever you want the text to change. Calling it once and then changing the variable you passed into the method will have no effect.
Re: Passing dynamic variables during runtime
Follow coding conventions. Variable names should start with a lowercase letter.
db