I am creating a Java application where I am querying different data in an api that I then pass to fields of type jLabel, but I need to make the user be able to choose how often these data will be updated, for this I use a jTextField where a value in milliseconds and then this value would be used to replace the default timer that I now have of 1000ms.
The problem is that I can't get from another window that contains the jTextField to pass the value to the MainFrame that contains the default timer and the jLabels that are updated over time.
What I have and am trying is the following, first I create a default timer and the one that will be modified depending on the user value
Timer timeDefault = new Timer(1000, null);
int timeUserConfig = 0;
then I create a function that is started together with the application and this is the one that starts to update the jLabel
contained in the function setLabelText();
inside timeDefault.addActionListener
public void setUpdateTime(int timeUserConfig) {
this.timeUserConfig = timeUserConfig;
if (timeUserConfig == 0){
timeDefault.start();
timeDefault.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
setLabelText();
}
});
} else {
timeDefault.setDelay(timeUserConfig);
}
}
the above works fine, the problem is when I want to get timeUserConfig from another java swing window of a jFrame form that contains the jTextField
Next what happens in the other window that when pressing a button a new value for the timer
assigned by the user would be sent, this value would replace the value contained in timeUserConfig
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//get text of TextField
String textoFieldTimer = jTextField1.getText();
//send to main window and go to method setUpdateTime(timeUserConfig);
}
So here I have been stuck for a long time, I tried things but it did not allow me to pass the value, if anyone has any idea or way to do it, I would appreciate it, thanks.
I have to comment that if I do everything within the same window where the data is updated, I have no problem changing the value, therefore the setUpdateTime()
function works.