JavaFX - Check if a checkbox is ticked


I am trying to write some code to determine whether or not my checkbox is ticked, I am aware I can write something like to change its state to checked


But I want to write something along the lines of

   write login-username to config file
} else {
   clear the config file

How would I go about doing this? I've been trauling through Oracle documentation but have yet to find anything useful


4/5/2014 3:25:22 PM

Accepted Answer

It would be great if there was an easier way to do this but I have put together a workaround - when my application launches I perform an initial check to see if the config file is populated

    private Boolean populateUsername() {

    // Remember me string initialised
    String remember = "";

    // Check that our config file has some contents
    String checkConfig = configReader.readFile();

    // Check we have a string of legnth 1+, then set
    // the remember string
    if(checkConfig.length() > 0){
        tickEnabled = true;
        remember = checkConfig;

    } else {
        tickEnabled = false;

I then update a global Boolean tickEnabled which allows me to track the state of the checkbox, from there I can fire my updateConfig method which clears the file if the user doesnt want to be remembered, or rewrites to the file if they do.

Like I said above - simple work around but it would be nice if there was a way for me to check the state of checkbox directly!

4/5/2014 3:35:11 PM

Have you tried registering a listener to the "selected" property of the checkbox? It would look something like this:

yourCheckbox.selectedProperty().addListener(new ChangeListener<Boolean>() {

        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
            // TODO Auto-generated method stub

                // your checkbox has been ticked. 
                // write login-username to config file


                // your checkbox has been unticked. do stuff...
                // clear the config file

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow