-1

I am creating a JavaFX program. Iwas trying to create menu items with a for loop.
I have tried to set text to MenuItem by placing the variable to the parameter. Error = int cannot be converted to String.

 MenuButton mes = new MenuButton("Mês");
        for(int i=1; i<=10; i++){
            mes.getItems().add(new MenuItem(i));
        }
Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
lima
  • 45
  • 1
  • 7

1 Answers1

3

From the source code,

public MenuItem(String label) throws HeadlessException {
    this(label, null);
}

So, to construct an object of MenuItem you need to pass in a String as a label.

You'll need to change your code to

mes.getItems().add(new MenuItem(String.valueOf(i)));

to convert i to a String for calling the constructor.

Mohamed Anees A
  • 4,119
  • 1
  • 22
  • 35