2016-04-01 4 views
0

Dans mon programme, je veux que l'utilisateur sélectionne un fichier à définir dans ImageView en utilisant FileChooser. Ceci est le code du contrôleur:JavaFX: Modifier l'image à partir d'ImageView en utilisant le fichier de FileChooser

@FXML ImageView floorPlanImage;

FileChooser fileChooser; 
public void loadImage() 
{ 
    System.out.println("Load Image Button Pressed"); 
    fileChooser = new FileChooser(); 

    File file = fileChooser.showOpenDialog(drawingAnchorPane.getScene().getWindow()); 
    if (file != null) { 
     System.out.println("File Was Selected"); 
     floorPlanImage.setImage(new Image(file.getAbsolutePath())); 

    } 

} 

Je reçois l'erreur suivante:

Load Image Button Pressed 
File Was Selected 
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source) 
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) 
    at javafx.event.Event.fireEvent(Unknown Source) 
    at javafx.scene.Node.fireEvent(Unknown Source) 
    at javafx.scene.control.Button.fire(Unknown Source) 
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source) 
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source) 
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source) 
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) 
    at javafx.event.Event.fireEvent(Unknown Source) 
    at javafx.scene.Scene$MouseHandler.process(Unknown Source) 
    at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source) 
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source) 
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(Unknown Source) 
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source) 
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source) 
    at com.sun.glass.ui.View.notifyMouse(Unknown Source) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source) 
    at java.lang.Thread.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 sun.reflect.misc.Trampoline.invoke(Unknown Source) 
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source) 
    ... 61 more 
Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c 
    at javafx.scene.image.Image.validateUrl(Unknown Source) 
    at javafx.scene.image.Image.<init>(Unknown Source) 
    at application.GroupController.loadImage(GroupController.java:72) 
    ... 70 more 
Caused by: java.net.MalformedURLException: unknown protocol: c 
    at java.net.URL.<init>(Unknown Source) 
    at java.net.URL.<init>(Unknown Source) 
    at java.net.URL.<init>(Unknown Source) 
    ... 73 more 

Le fichier qui utilisateur de sélectionner ne fait pas partie des ressources de l'application.

Merci

+0

Il est de ne pas trouver l'image que vous essayez de mettre dans le fichier. –

+0

Je sais ... Mais pourquoi? Je reçois l'image en utilisant FileChooser. – Alex

+0

Le fichier que vous essayez de charger dans votre chemin de classe ou est-ce un fichier externe? –

Répondre

5

Le constructeur Image attend une chaîne représentant l'URL de l'image (et non le chemin du fichier).

Vous pouvez faire

URL url = file.toURI().toURL(); 
floorPlanImage.setImage(new Image(url.toExternalForm())); 
+0

Je reçois la "La méthode toURI() est indéfinie pour le type String". La configuration du projet est Java 1.8 – Alex

+0

Yup, désolé: erreur de copier-coller. Voir mise à jour. –

+0

Merci! Ça marche – Alex