在Javafx

时间:2018-08-05 05:47:27

标签: javafx checkbox

是否可以更改javafx中三态复选框的点击顺序? 默认值为check-> uncheck-> indeterminate。
可以改为具有以下顺序吗?检查->不确定->取消选中?

关于为什么的一些背景。

使用该复选框的应用会将复选框默认设置为false,这对应于需要但尚未被选中的进程。
未定义状态对应于经过物理检查并确定为不良的进程。
 检查状态对应于经过物理检查并通过QC的进程。
 真正的案例比未定义的案例更常见。

由于默认值为false,因此循环到的下一次单击是不确定的。 (错误),然后为true(检查)。这些点击是经过几个过程完成的,更改顺序将大大减少用户必须点击的次数。

1 个答案:

答案 0 :(得分:0)

感谢您帮助我提出建议。我现在找到了解决这个问题的方法。
CheckBox类中订单的循环基于以下逻辑 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

我创建了CheckBox类的扩展并覆盖了fire()方法
下面是CheckBox的版本,现在可以根据需要循环运行。

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());


        }
    }

}