java - Simplest way to add an image in Javafx? -
i don't understand how add simple image. imported , followed said on page:
http://www.java2s.com/code/java/javafx/loadajpgimagewithimageanduseimageviewtodisplay.htm
javafx code
import javafx.application.application; import javafx.scene.group; import javafx.scene.scene; import javafx.scene.image.image; import javafx.scene.image.imageview; import javafx.scene.layout.vbox; import javafx.stage.stage; public class test extends application { @override public void start(stage stage) { stage.settitle("html"); stage.setwidth(500); stage.setheight(500); scene scene = new scene(new group()); vbox root = new vbox(); final imageview selectedimage = new imageview(); image image1 = new image(test.class.getresourceasstream("c:\\users\\user\\desktop\\x.jpg")); selectedimage.setimage(image1); root.getchildren().addall(selectedimage); scene.setroot(root); stage.setscene(scene); stage.show(); } public static void main(string[] args) { launch(args); } }
error
@ com.sun.javafx.application.launcherimpl.lambda$launchapplication$156(launcherimpl.java:182) @ java.lang.thread.run(thread.java:745) caused by: java.lang.nullpointerexception: input stream must not null @ javafx.scene.image.image.validateinputstream(image.java:1110) @ javafx.scene.image.image.<init>(image.java:694) @ prototype.test.start(test.java:23) @ com.sun.javafx.application.launcherimpl.lambda$launchapplication1$163(launcherimpl.java:863) @ com.sun.javafx.application.platformimpl.lambda$runandwait$176(platformimpl.java:326) @ com.sun.javafx.application.platformimpl.lambda$null$174(platformimpl.java:295) @ java.security.accesscontroller.doprivileged(native method) @ com.sun.javafx.application.platformimpl.lambda$runlater$175(platformimpl.java:294) @ com.sun.glass.ui.invokelaterdispatcher$future.run(invokelaterdispatcher.java:95) @ com.sun.glass.ui.win.winapplication._runloop(native method) @ com.sun.glass.ui.win.winapplication.lambda$null$149(winapplication.java:191)
class.getresourceasstream
tries load element in classpath. it's not meant load files unless included in classpath. load file outside of classpath, use fileinputstream
instead:
image image1 = new image(new fileinputstream("c:\\users\\user\\desktop\\x.jpg"));
or use image(string)
constructor , pass url
of file:
image image1 = new image(new file("c:\\users\\user\\desktop\\x.jpg").touri().tourl().toexternalform());
Comments
Post a Comment