문제

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

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top