Inserting image into ImageView using Javafx -
i have frame controller class reading values form(which created using javafx scene builder) , inserting information after controlling validity. want know how insert image imageview element on stage.
public class framecontroller { @fxml private imageview img_view; .... @fxml public void buttonclicked() { ... img_view = new imageview(); image img = new image("../img/img.jpg"); img_view.setimage(img); ... } }
this not work. runs debugger after clicked button bound buttonclicked() function. missing something? how can insert image imageview element on scene? appreciate help.
something fix:
- the relative path reference
../
not resolve url can loaded jar protocol (which standard protocol used loading resources in packaged javafx apps). don't use relative reference..\
in it. instead use absolute reference, example:
/com/mycompany/myapp/myproject/img/img.jpg
some things check (assuming have made change above):
- load fxml using
fxmlloader
. - in fxml have
fx:controller
attribute references framecontroller. - in fxml have imageview element defines
fx:id
img_view (ensurefx:id
, notid
). - understand image loading rules outlined in sergey's answer to: where javafx.scene.image.image("flower.png") flower.png?.
in controller, add line:
system.out.println(framecontroller.getclass().getresource( "/com/mycompany/myapp/myproject/img/img.jpg").toexternalform() );
this print out system looking image.
ensure build system copying img.jpg class output directory. this, after build completes , assuming build makes jar named myapp.jar, run:
jar xvf myapp.jar
and see if img.jpg @ path com/mycompany/myproject/img/img.jpg.
after create img object, call:
img.iserror();
to see if there loading error image.
Comments
Post a Comment