10

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.

xigoa
  • 109
  • 1
  • 5
  • Adding a path using the scene builder dialog definitely works. Where are you specifying the classpath when asked? If you are using eclipse it needs to be the bin directory. – Andy Till May 21 '13 at 20:44
  • Yes, I'm specifying the bin directory, after compiling the project. But it doesn't work for me... I have it as follows: – xigoa May 22 '13 at 06:38
  • It might be a good idea to read up on the java class path. If you have a class with namespace libreria.MyComponent then you need to add the parent directory of libreria and not the full path. Try although I always use the dialog. – Andy Till May 22 '13 at 09:06
  • I have used the dialog actually. It seems to be everything ok but SceneBuilder doesn't show the custom component on the hierarchy panel, the import doesn't work neither way I specify it or the classpath. I have tried the way you have suggested with the same result... – xigoa May 22 '13 at 10:10
  • All I can think of is that the relative path is not correct. – Andy Till May 22 '13 at 16:30
  • I have edited the question with more information. I've done a new and clean project and the problem persists. It seems that I'm doing something wrong but I don't get it. – xigoa May 23 '13 at 07:12
  • We have tried it in Eclipse and it works properly, but not in SceneBuilder, and it would be very important for our project to get this. Thanks Andy for your help, appreciate it. – xigoa May 24 '13 at 07:36
  • There is an error in your mini example where you specify the namespace directory again 'custom', it should just be bin. Other than that I am not sure. – Andy Till May 24 '13 at 12:02
  • Andy, please put this as an answer because at last it works and I'll be glad to give you the points. Thank you very much indeed for your help! – xigoa May 24 '13 at 12:20
  • I have exactly the same problem with my Scene Builder 1.1 and an Eclipse build set of Java classes. I have specified the "bin" folder of as the root of the path to the classes but still get the same class not found error. – Kolban Apr 20 '14 at 15:27

2 Answers2

4

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.

Andy Till
  • 3,371
  • 2
  • 18
  • 23
1

maybe it is late but I would like to tell what I did. I am using J8, Eclipse IDE and Scenebuilder 2.0.

1) On Eclipse IDE, open Navigator view from Window->Show View > Navigator 2) On your project, right click and select "Properties" on the class you want to see on SB (Scene Builder) . Verify the location of the generated class from the Resource section. For example you shuould see the location as in the following. /TableViewDemo/bin/com/company/jfx8/example/fxmltableview/FormattedTableCellFactory.class

3) Copy this address and add fxml such as

<?scenebuilder-classpath-element ../../../../TableViewDemo/bin/com/company/jfx8/example/fxmltableview/FormattedTableCellFactory.class?> 

4) Then save it and enjoy your work:)