Pergunta

as I mentioned in a post before, I'm porting my program to Java, to make it available for Mac OS and Linux users.

At the start of the program, I'd like to check if adb is installed to the system using this code:

private void checkADBExists()
// Checks if adb binaries exist and sets jTogglebutton1 correspondingly...
{
    File adb = new File("/usr/bin/adb");
    if (!adb.exists())
    {
        jToggleButton1.isSelected();
    } else {
        jToggleButton1.isSelected()= false;
    }
}

Here's my problem: If the file doesn't exist, the JToggleButton isn't selected, even though it should be and I get an error deselecting it.

Any help is much appreciated.

Thanks in advance, Beats

Foi útil?

Solução

Many of Swing's core components follow a simple getter/setter pattern.

That is, you can "get" a property value and "set" a property value (note, not all getters have a corresponding setter though).

In the case of a boolean property, the convention is to use "is" instead of "get", it just rolls off the tongue better.

So, in your case, all you are doing is getting the value if the selected property, not really what you want to do.

Instead use jToggleButton1.setSelected(true) or jToggleButton1.setSelected(false) based on your needs

You might like to take a look at How to Use Buttons, Check Boxes, and Radio Buttons for some more details

Outras dicas

JToggleButton().isSelected() return a value not a variable. By JToggleButton().isSelected() = false, you are trying to assign a value to a value, it doesn't make sense, much like writing a statement 2 = 2;. use JToggleButton.setSelected(true) to set the toggle button as selected and JToggleButton.setSelected(false) to deselect.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top