0

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

SimonC
  • 1,547
  • 1
  • 19
  • 43
  • You can set a value to the return value of a function. – Obicere Oct 25 '13 at 21:10
  • You need to use [setSelected(boolean selected)](http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setSelected%28boolean%29) instead. – dic19 Oct 25 '13 at 21:24
  • @Beatsleigher see Sage's answer. He addresses what I mean. It's the `jToggleButton1.isSelected()= false;`. – Obicere Oct 26 '13 at 01:53

2 Answers2

3

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Oh. Thanks. I didn't see the setSelected() method. – SimonC Oct 25 '13 at 21:32
  • @Beatsleigher, So how about accepting the answer in this posting and your other postings (if you want to continue to get help in the future). – camickr Oct 25 '13 at 23:52
1

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.

Sage
  • 15,290
  • 3
  • 33
  • 38