Search code examples
javajcheckbox

how to write a code to set a boolean value either true or false in java swings


I want to set a boolean value to either true or false.I kept data base type as java.lang.boolean. and I want to write a code If the checkbox is selected It should be saved and I should be able to see in the frontend part. Can anyone suggest me with the right code. Below is my code

public Boolean setSelected(Boolean abool)
{
    if (abool=="Y")
        abool = true;
    else
        abool = false;

}

In this code I am missing something and getting error Incompatible operand types Boolean and String.


Solution

  • From the Docs:

    boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

    In your code you're trying to compare a string value with a boolean which caused exception. You can use something like below to compare a string value and return a boolean.

     public boolean setSelected(String abool){
            boolean status = false;
            if ("Y".equals(abool))
                status = true;
            return status;
        }