Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX custom control (TextField) not working

I am trying to make a custom control with JavaFX and SceneBuilder 1.1.

I have this code:

FXML


<?import libreria.javaFX.componentes.componenteTextField.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
  <children>
    <CustomComponent fx:id="pastaTxt" layoutX="69.0" layoutY="87.0" prefWidth="200.0" />
  </children>
</AnchorPane>

CustomComponent.java


package libreria.javaFX.componentes.componenteTextField;

import javafx.scene.control.TextField;

public class CustomComponent extends TextField {

public CustomComponent() {
    super();
    // TODO Auto-generated constructor stub
}

public CustomComponent(String arg0) {
    super(arg0);
    // TODO Auto-generated constructor stub
}

}


When I try to open it from SceneBuilder it tells me this:

Missing types are: [CustomComponent]

and it gives me the chance to specify the Classpath (which doesn't fix the problem either).

I tried putting the class at the import statement too, like this:

<?import libreria.javaFX.componentes.componenteTextField.CustomComponent?>

But it gives a ClassNotFoundException.

Any ideas about why is this happening?


MORE INFORMATION

I have done a new project with just these classes:

enter image description here

And the code is as follows:

CustomControl.fxml

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

<?import custom.CustomControl?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?scenebuilder-classpath-element ../../bin/custom?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
  <children>
    <CustomControl layoutX="51.0" layoutY="100.0" prefWidth="200.0" />
  </children>
</AnchorPane>

CustomControl.java

package custom;

import javafx.scene.control.TextField;

public class CustomControl extends TextField {

    public CustomControl() {
        super();
    }

public CustomControl(String arg0) {
    super(arg0);

    }
}

And I still have the same problem. I specify the classpath with the dialog, everything seems right to me but I have the same errors opening the SceneBuilder.


LAST INFORMATION

Trying to approach to the solution, we tried this project under Eclipse. The result is that Eclipse shows the window ok but SceneBuilder continues with those errors. I hope this clue helps.

If anyone has done this kind of custom control definition under Scene Builder, please, tell us and give us an example, it will be extremely helpful for our project.

like image 876
xigoa Avatar asked May 21 '13 15:05

xigoa


1 Answers

This is caused by not specifying the correct classpath, which allows the java runtime running scene builder to load the control classes.

If you are running eclipse and your class has the namespace custom.MyControl then specify the bin directory and not the custom directory. In a maven project you need to specify the target/classes directory.

See an example in my own project here: https://bitbucket.org/atill/estimate/src/22390a2ca034b55f1916e46435b714e5c489b90e/src/main/resources/projmon/gui/workTree.fxml?at=master

A relative file path is often created by scene builder so moving files will break the class path and you will need to respecify it.

like image 86
Andy Till Avatar answered Sep 17 '22 23:09

Andy Till