7 Replies - 236 Views - Last Post: 02 July 2020 - 01:39 PM Rate Topic: -----

#1 Ex_Nihilo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-June 20

Java boolean not changing value

Posted 29 June 2020 - 06:33 AM

Hi, i'm trying to build a java chronometer but am having trouble changing the value of a boolean variable.

I declared it as false at the beginning of my program, and when I press the start button i want it to be set to true.

Posted Image

public class App extends Application {

    public boolean isRunning = false;


timeline = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println(isRunning);
                while(isRunning){
                    updateLabel();
                }

            }
        }));


@FXML
    public void kronosStart(){
        System.out.println("Start button pressed");
        startTime = System.currentTimeMillis();
        isRunning = true;
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Java boolean not changing value

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: Java boolean not changing value

Posted 29 June 2020 - 06:45 AM

Is there more than one variable named isRunning?
Is there more than one instance of the class that contains isRunning allowing the code to set its value in one instance and print the value in the other instance?

This post has been edited by NormR: 29 June 2020 - 06:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 idlewild   User is online

  • New D.I.C Head

Reputation: 3
  • Posts: 35
  • Joined: 19-June 20

Re: Java boolean not changing value

Posted 29 June 2020 - 07:18 AM

in your code where you want to do that

why do not you
isRunning = !isRunning;

Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: Java boolean not changing value

Posted 29 June 2020 - 07:27 AM

Two different concepts
Always turn switch ON
Change switch's current setting

Which one works depends on the program's requirement.
Was This Post Helpful? 0
  • +
  • -

#5 Ex_Nihilo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-June 20

Re: Java boolean not changing value

Posted 30 June 2020 - 07:01 AM

Thanks everyone, i still don't understand why it's not changing value but I managed to remove that variable completely from my program and it still works :)
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: Java boolean not changing value

Posted 30 June 2020 - 07:27 AM

If the program works without that variable, what was the the original reason for having it in the program?
Was This Post Helpful? 0
  • +
  • -

#7 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3698
  • View blog
  • Posts: 16,952
  • Joined: 20-September 08

Re: Java boolean not changing value

Posted 30 June 2020 - 07:43 AM

I haven't used that kind of timer with JavaFX but i'm guessing you would not want a while loop in handle (it's probably already in a loop)
Was This Post Helpful? 0
  • +
  • -

#8 Ex_Nihilo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-June 20

Re: Java boolean not changing value

Posted 02 July 2020 - 01:39 PM

View PostNormR, on 30 June 2020 - 07:27 AM, said:

If the program works without that variable, what was the the original reason for having it in the program?


I wanted to have a variable to control the state of my chronometer, but that was unnecessary using the timeline.play() and timeline.stop().
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1