Search code examples
javafor-loopjtextfield

Creating Textfields by loop


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

Solution

  • 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());