0

When using a for loop to add menu items to my "Connections" menu, I get the following error with the InteliJ IDEA:

Cannot Set javafx.scene.control.Menu to field "menuConnection"

I think this is being caused by some kind of naming collision. Very new to Java so can't be certain.

So here is my FXML file:

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox xmlns="http://javafx.com/javafx"
  xmlns:fx="http://javafx.com/fxml"
  fx:controller="kuristo.Controller"
  prefHeight="400.0" prefWidth="600.0">

  <MenuBar>
    <Menu text="File">
        <MenuItem text="Quit" />
    </Menu>
    <Menu text="Connection" fx:id="menuConnection"></Menu>
</MenuBar>

</VBox>

So as you can see in this file I have a Menu with the id of menuConnection. I am using this as a means to identify the Menu I wish to add MenuItems to in this controller:

package kuristo;

import com.fazecast.jSerialComm.SerialPort;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

import java.awt.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @FXML
    private Menu menuConnection;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        for (int i = 0; i < SerialPort.getCommPorts().length; i++) {

            // String portName = "portName".concat(String.valueOf(i));

            MenuItem port = new MenuItem(SerialPort.getCommPorts()[i].getSystemPortName());

            menuConnection.add(port);

        }

    }

}

I can't get this to compile. Any help would be much appreciated.

Cheers,

Clayton Allen
  • 239
  • 3
  • 15

1 Answers1

1

As it turns out...

My imports were incorrect.

import java.awt.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

Changed to:

import com.fazecast.jSerialComm.SerialPort;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;

import java.net.URL;
import java.util.ResourceBundle;

Moral of the story here, make sure you're using JavaFX not AWT.

Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
Clayton Allen
  • 239
  • 3
  • 15