JavaFX 2: How to Load Image

This is JavaFX tutorial about how to load a image in your JavaFX 2 application. This can be done easily with ImageView. The ImageView is a Node used for painting images loaded with Image class. So as you can se we will first load image with Image class and then display it with ImageView. Also I will here demonstrate how to load image from local disk, and how to load image from Internet. First example is how to load an image from disk, then I’ll show how to modify it, to load image from Internet.
 
 
 
 
 

Load Image with ImageView in JavaFX 2

Load Image with ImageView in JavaFX 2

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    /**
    *
    * @author zoranpavlovic.blogspot.com
    */
    public class LoadImage extends Application {
            /**
            * @param args the command line arguments
            */
            public static void main(String[] args) {
            Application.launch(args);
            }
            @Override
            public void start(Stage primaryStage) {
            primaryStage.setTitle("Load Image");

            StackPane sp = new StackPane();
            Image img = new Image("javafx.jpg");
            ImageView imgView = new ImageView(img);
            sp.getChildren().add(imgView);

            //Adding HBox to the scene
            Scene scene = new Scene(sp);
            primaryStage.setScene(scene);
            primaryStage.show();
    }
    }

Okay, what if you want to load a image from some Internet location? Well, that can be also easily done, just modify this line of code. The result will be same.

    Image img = new Image("http://mikecann.co.uk/wp-content/uploads/2009/12/javafx_logo_color_1.jpg");

 

Reference: JavaFX 2: How to Load Image from our JCG partner Zoran Pavlovic at the Zoran Pavlovic blog blog.
Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

One Response to "JavaFX 2: How to Load Image"

  1. Pranish says:

    What if I want to dynamically choose the file from filechooser and upload it in the imageview

Leave a Reply


+ three = 10



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books