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
    }
}

2 Answers 2

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());
Sign up to request clarification or add additional context in comments.

4 Comments

.getName() does not seem to work I get the error cannot find symbol symbol: method getname() location: variable direction of type volstextfield
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.
i did with .name, i am now looking at your suggestion of defining the name to vol1hh1("vol1hh1")
I am not clear as to why defining the names like that would be different? or how to do that?
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());
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.