0

I have the following list of items in volstextfield which I then pass onto createGUI. I want to create a list of JTextFields with the names vol1HH1,vol1HH2. I get an error Incompatible types: JTextField cannot be converted to volstextfield, please can someone help?

public enum volstextfield {
    vol1HH1, vol1HH2
}

public void createGUI() {
    for (volstextfield direction : EnumSet.allOf(volstextfield.class)) {
        System.out.println(direction);
        direction = new JTextField(5); //i get an error here incompatible types: JTextField cannot be converted to volstextfield
    }
}
akash
  • 22,664
  • 11
  • 59
  • 87
Ingram
  • 654
  • 2
  • 7
  • 29

2 Answers2

1

Your direction variable is of enum type. You cannot assign JTextField to enum. Try like

JTextField textfield = new JTextField(direction.getName());

Or use

JTextField textfield = new JTextField();
textfield.setName(direction.getName());
JackWhiteIII
  • 1,388
  • 2
  • 11
  • 25
Garry
  • 4,493
  • 3
  • 28
  • 48
  • .getName() does not seem to work I get the error cannot find symbol symbol: method getname() location: variable direction of type volstextfield – Ingram Jul 05 '15 at 11:13
  • You neeb bit modification in you enum too, try define like vol1hh1("vol1hh1") and add name variable to enum and add toString to return the name. It will work. – Garry Jul 05 '15 at 11:18
  • i did with .name, i am now looking at your suggestion of defining the name to vol1hh1("vol1hh1") – Ingram Jul 05 '15 at 11:49
  • I am not clear as to why defining the names like that would be different? or how to do that? – Ingram Jul 05 '15 at 11:53
0

Enum is a type itself, the closest thing you could do is :

 // The Amount of JTextField is enum' length 
  JTextField direction[] = new JTextField[volstextfield .values().length]; 
        int i=0;

        for ( volstextfield v :volstextfield.values()) {
            direction [i++] = new JTextField(v.name());
        }
Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19