0

I am developing a car application which has option to select car features with JCheckBox and there are about 30 JCheckBox.

As this is a Database Application, I need to get and set selected checkboxes in Database overtime.

On Stack Overflow I found this question which is similar to my requirements but this doesn't work in my case https://stackoverflow.com/a/19246403/4099884

I am adding my check boxes like this:

package haams;

import java.util.List;
import java.util.ArrayList;

public final class HAAMS {

    JCheckBox AirConditioner = new JCheckBox();;
    JCheckBox ClimateControl = new JCheckBox();;
    JCheckBox AntiLockBrakes = new JCheckBox();;

    List<JCheckBox> CarFeatures = new ArrayList<JCheckBox>();

    CarFeatures.add(AirConditioner);  //Error: Package CarFeatures does not exists
    CarFeatures.add(ClimateControl);  //Error: Package CarFeatures does not exists
    CarFeatures.add(AntiLockBrakes);  //Error: Package CarFeatures does not exists

    public static void main(String[] args) {}
}

Why it is saying Package CarFeatures does not exists? What am I doing wrong?

Community
  • 1
  • 1

2 Answers2

2

These lines:

CarFeatures.add(AirConditioner);  //Error: Package CarFeatures does not exists
CarFeatures.add(ClimateControl);  //Error: Package CarFeatures does not exists
CarFeatures.add(AntiLockBrakes);  //Error: Package CarFeatures does not exists

need to be inside a method or a static initializer. Perhaps add a constructor or a main method and place the code there.

You should also use camelCase for variable names. So carFeatures would be the right choice.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
1

The problem is that all your code it's not even in a class causing a compiling error. On the other hand method calls should be done in a method/constructor context. These lines:

CarFeatures.add(AirConditioner);
CarFeatures.add(ClimateControl);
CarFeatures.add(AntiLockBrakes);

Have to be included in class constructor or some other method:

public MyClass() {
    CarFeatures.add(AirConditioner);
    CarFeatures.add(ClimateControl);
    CarFeatures.add(AntiLockBrakes);
}

// or

private void addCarsFeatures() {
    CarFeatures.add(AirConditioner);
    CarFeatures.add(ClimateControl);
    CarFeatures.add(AntiLockBrakes);
}
dic19
  • 17,821
  • 6
  • 40
  • 69
  • 1
    I didn't even know this. Shame on me. Thanks to you. How stupid of me. Actually I didn't even know this. This is what happens when you just keep using NetBeans Drag and Drop –  Oct 03 '14 at 13:18