returncode13
returncode13

Reputation: 11

Change the cycle order in a three state Checkbox in javafx

Is there a way to change the click order for the three-state Checkbox in javafx? The default is check->uncheck->indeterminate.
Is it possible to instead have the following order? check->indeterminate->uncheck ?

Some background on the why.

The app that uses the checkbox defaults the checkbox to false which corresponds to a process that needs to be but hasn't yet been checked.
The undefined state corresponds to a process that was physically checked and has been determined as bad.
The checked state corresponds to a process that was physically checked and has passed QC.
The true cases occur more often than the undefined cases.

Since the default is false, the next click cycled to is undefined. (bad) and then true(check). These clicks are made for several process and changing the order will considerably reduce the number of clicks that has to be made by the user.

Upvotes: 0

Views: 278

Answers (1)

returncode13
returncode13

Reputation: 11

Thank you for helping me with your suggestions. I have now found an answer to this problem.
The cycling of the order in the CheckBox class is based on the following logic https://docs.oracle.com/javafx/2/api/javafx/scene/control/CheckBox.html

checked: indeterminate == false, checked == true
unchecked: indeterminate == false, checked == false
undefined: indeterminate == true

I created an extension to the CheckBox class and overrode the fire() method
Below is the version of the CheckBox which now cycles as required.

import javafx.event.ActionEvent;
import javafx.scene.control.CheckBox;

/**
 *
 * @author returncode13
 */
public class RcheckBox extends CheckBox{


    /**
     * Toggles the state of the {@code CheckBox}. If allowIndeterminate is
     * true, then each invocation of this function will advance the CheckBox
     * through the states checked, undefined, and unchecked. If
     * allowIndeterminate is false, then the CheckBox will only cycle through
     * the checked and unchecked states, and forcing indeterminate to equal to
     * false.
     */

    @Override
    public void fire() {
        super.fire(); 
        if(!isDisabled()){

            if(isAllowIndeterminate()){
                 if(!isSelected() && !isIndeterminate()){
                    setIndeterminate(true);
                }else if(isIndeterminate()){
                    setSelected(true);
                    setIndeterminate(false);
                }else if(isSelected() && !isIndeterminate()){
                    setSelected(false);
                    setIndeterminate(false);
                }
            }else{
                setSelected(!isSelected());
                setIndeterminate(false);
            }
            fireEvent(new ActionEvent());


        }
    }

}

Upvotes: 0

Related Questions