Skip to main content

Problem loading dynamically JavaFX CustomNode

5 replies [Last post]
nidaya
Offline
Joined: 2006-07-29

Hi,

I would like to load dynamically a JavaFX CustomNode class using an URLClassLoader and display it in an applet.
But I have a ClassCastException :

"java.lang.ClassCastException: javafxlib.HelloWorld cannot be cast to javafx.scene.Node$Intf
at javafxapp.DynaApp.javafx$run$(DynaApp.fx:26)"

Could you help me, please ?

This is my code :

--------------------------------
javafxlib.HelloWorld.fx compiled in JavaFXLib.jar :
--------------------------------
package javafxlib;

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.text.Text;
import javafx.scene.Font;
import javafx.scene.FontStyle;

public class HelloWorld extends CustomNode {

public function create(): Node {
return Group {
content: [
Text {
font: Font {
size: 24
style: FontStyle.PLAIN
}
x: 10, y: 30
content: "HelloWorld"
}]
};
}
}

--------------------------------
javafxapp.DynaApp.fx compiled in a signed JavaFXApp.jar :
--------------------------------
package javafxapp;

import javafx.scene.Node;
import javafx.application.Application;
import javafx.ext.swing.Canvas;
import java.lang.Class;
import java.lang.System;
import java.net.URL;
import java.net.URLClassLoader;

var urls: URL[];
try {
insert new URL("http://localhost:11000/QuartisAS-web/jars/lib/javafxc.jar") into urls;
insert new URL("http://localhost:11000/QuartisAS-web/jars/lib/javafxrt.jar") into urls;
insert new URL("http://localhost:11000/QuartisAS-web/jars/lib/Scenario.jar") into urls;
insert new URL("http://localhost:11000/QuartisAS-web/jars/lib/javafx-swing.jar") into urls;
insert new URL("http://localhost:11000/QuartisAS-web/jars/lib/javafxdoc.jar") into urls;
insert new URL("http://localhost:11000/QuartisAS-web/jars/lib/javafxgui.jar") into urls;
insert new URL("http://localhost:11000/QuartisAS-web/jars/lib/jmc.jar") into urls;
insert new URL("http://localhost:11000/QuartisAS-web/jars/lib/JavaFXLib.jar") into urls;
} catch(any) {
System.out.println("URL Exception : {any}");
}

var classLoader: URLClassLoader = new URLClassLoader( urls );
var helloWorld = classLoader.loadClass("javafxlib.HelloWorld").newInstance() as Node;

Application {
content: Canvas {
content: [ helloWorld ]
}
}

--------------------------------
This is the exception :
--------------------------------
Exception in thread "AWT-EventQueue-3" java.lang.Error: Couldn't launch FX Application javafxapp.DynaApp
at javafx.application.Applet.launchApplication(Applet.fx:65)
at javafx.application.Applet$1.invoke(Applet.fx:79)
at javafx.application.Applet$1.invoke(Applet.fx:79)
at javafx.lang.DeferredTask$1.lambda(DeferredTask.fx:41)
at javafx.lang.DeferredTask$1.invoke(DeferredTask.fx:41)
at javafx.lang.DeferredTask$1.invoke(DeferredTask.fx:41)
at com.sun.javafx.runtime.Entry$1.run(Entry.java:71)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at javafx.application.Applet.launchApplication(Applet.fx:61)
... 14 more
Caused by: java.lang.ClassCastException: javafxlib.HelloWorld cannot be cast to javafx.scene.Node$Intf
at javafxapp.DynaApp.javafx$run$(DynaApp.fx:26)
... 19 more

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
nidaya
Offline
Joined: 2006-07-29

Thanks alexsch,

I found a solution ! I don't think it's the best, but it works !

On the server-side, I have a JavaFX Component loader class that loads dynamically JavaFX CustomNode using a classloader as parameter and converts it in JComponent.

On the applet-side (JApplet), I create my URLClassloader to load the JavaFX Component loader class and invoke the CustomNode > JComponent method with the same URLClassloader. Then, I add the JComponent result to the contentPane.

Thanks again !

alexsch
Offline
Joined: 2007-07-12

I guess that problem can be because you load HelloWorld class using URLClassLoader
and the Application is loaded by SystemClassLoader.
The classes are not cast to each other in Java if they are loaded by different class loaders.

The solution can be loading all classes by URLClassLoader.

nidaya
Offline
Joined: 2006-07-29

Thanks for your answer,

I think it's a very good idea and I would like to test it, but how can I change the Application's classloader knowing that I use it in html page this applet tags and a jnlp file ?

In fact, I would like to load and display dynamically in an applet (JavaFX Application or JApplet), JavaFX CustomNodes generated programmatically in a jar file on the server-side.

Thanks again to help me.

PS : I hope my english is good enough to explain you what I would like to do !

alexsch
Offline
Joined: 2007-07-12

Which version of JavaFX Script/NetBeans plugin do you use?

nidaya
Offline
Joined: 2006-07-29

Thanks to help me !

I am using NetBeans 6.1 and JavaFx-NetBeans-Plugin daily built the 3rd July 2008.