Netbeans says class does not exist in FXML file for observablearrayList but it does


Question

I have this FXML file generated by Netbeans (but I have modified the TableColumn bit):

    <?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.collections.*?>


<AnchorPane id="AnchorPane" fx:id="MainPane" prefHeight="529.0" prefWidth="513.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="d1example2.UserInterfaceController">
  <children>
    <SplitPane dividerPositions="0.17835671342685372" focusTraversable="true" layoutX="14.0" layoutY="14.0" orientation="VERTICAL" prefHeight="501.0" prefWidth="485.0">
      <items>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
          <children>
            <AnchorPane id="AnchorPane" layoutX="95.0" layoutY="32.0">
              <children>
                <TextField fx:id="FirstField" layoutX="0.0" layoutY="0.0" prefWidth="51.0" />
                <Text fx:id="TimesText" layoutX="65.0" layoutY="16.0" strokeType="OUTSIDE" strokeWidth="0.0" text="*" />
                <TextField id="FirstField" layoutX="90.0" layoutY="0.0" prefWidth="51.0" />
                <Text fx:id="EqualSign" layoutX="168.0" layoutY="16.0" strokeType="OUTSIDE" strokeWidth="0.0" text="=" />
                <Text fx:id="EquationResult" layoutX="245.0" layoutY="16.0" scaleX="1.632154219166214E8" scaleY="1.436917974231407" strokeType="OUTSIDE" strokeWidth="0.0" text="" />
              </children>
            </AnchorPane>
            <Button fx:id="SubmitButton" layoutX="356.0" layoutY="32.0" mnemonicParsing="false" onMouseClicked="#handleButtonAction" text="Submit" />
          </children>
        </AnchorPane>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
          <children>
            <TableView layoutX="13.0" layoutY="14.0" prefHeight="379.0" prefWidth="456.0">
              <columns>
     <TableColumn text="Half Value">
        <cellValueFactory><PropertyValueFactory property="HalfContent" />
        </cellValueFactory>
     </TableColumn>
     <TableColumn text="Double Value">
        <cellValueFactory><PropertyValueFactory property="DoubleContent" />
        </cellValueFactory>
     </TableColumn>
</columns> 
              <items>
            <FXCollections fx:factory="observableArrayList">
                <Data HalfContent="First Column Text" DoubleContent="Second Column Text" />
            </FXCollections>
            </items>
            </TableView>
          </children>
        </AnchorPane>
      </items>
    </SplitPane>
  </children>
</AnchorPane>

And this other class called "Data" which looks like this:

package d1example2;

import javafx.beans.property.SimpleStringProperty;

/**
 *
 * @author zmeshign
 */
public final class Data {
   private final SimpleStringProperty HalfContent = new SimpleStringProperty("");
   private final SimpleStringProperty DoubleContent = new SimpleStringProperty("");

    public Data() {
        this("", "");
    }

    public Data(String HalfContent, String DoubleContent) {
        setHalfContent(HalfContent);
        setDoubleContent(DoubleContent);
    }

    public String getHalfContent() {
        return HalfContent.get();
    }

    public void setHalfContent(String hContent) {
        HalfContent.set(hContent);
    }

    public String getDoubleContent() {
        return DoubleContent.get();
    }

    public void setDoubleContent(String dContent) {
        DoubleContent.set(dContent);
    }

}

So in this line:

<Data HalfContent="First Column Text" DoubleContent="Second Column Text" />

it says that the class "Data" does not exist while I have it under the same package. All I'm trying to do is to learn how to display a string under a column in a TableView.

I would immensely appreciate any help! Thanks

1
1
12/3/2013 9:14:19 PM

Accepted Answer

How to fix it

In your case the appropriate directive you need to add to the FXML file in order for it to find your Data class is:

<?import d1example2.Data?>

Why it was failing for you

FXML files are just files, they are not compiled to Java classes (at least not in the JavaFX 2.2 version). This means that the files themselves aren't really aware of what package they are in (as packaging is a Java concept). The FXML files can be told where to locate the Java classes they need though. To do this you use the <?import java.lang.*?> directive at the top of the file (substituting your package name to perform the appropriate import).


Update based on additional question

This fixed the problem with the "data" part, but now it's saying: Class d1example2.Data does not support property "HalfContent". It also says this with "DoubleContent".

You need to follow Java property naming conventions. Under these conventions property names start with a lower case letter rather than an uppercase letter, so use halfContent and doubleContent as your property names in FXML.

Instead of:

<Data HalfContent="First Column Text" DoubleContent="Second Column Text" />

Write:

<Data halfContent="First Column Text" doubleContent="Second Column Text" />

Additionally, you might want to define property accessors for your properties:

public StringProperty halfContentProperty() {
    return halfContent;
}
. . . 
2
12/5/2013 7:27:22 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon