Insert image into borderpane as background


I want to load image into BorderPane. I have a static Java method which makes this task more complicated for me:

private static final Image iv;    
    static {
        iv = new Image(StartPanel.class.getClass().getResource("/images/system-help.png").toExternalForm());

public static void insert(){


Can you tell me how I can load the image properly?

P.S I get this

Executing com.javafx.main.Main from /home/rcbandit/Desktop/test/DX-57DC/dist/run1833589211/DX-57DC.jar using platform /opt/jdk1.7.0_25/bin/java
Exception in Application start method
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at com.javafx.main.Main.launchApp(
    at com.javafx.main.Main.main(
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(
    at com.sun.javafx.application.LauncherImpl.access$000(
    at com.sun.javafx.application.LauncherImpl$
Caused by: java.lang.NullPointerException
    at com.dx57dc.stages.StartPanel.infrastructureIcon(
    at com.dx57dc.stages.StartPanel.navigationPanel(
    at com.dx57dc.stages.StartPanel.agentsPanel(
    at com.dx57dc.stages.Agents.agentsPanel(
    at com.dx57dc.main.DX57DC.initMainStage(
    at com.dx57dc.main.DX57DC.start(
    at com.sun.javafx.application.LauncherImpl$
    at com.sun.javafx.application.PlatformImpl$
    at com.sun.javafx.application.PlatformImpl$4$
    at com.sun.javafx.application.PlatformImpl$4$
    at Method)
    at com.sun.javafx.application.PlatformImpl$
    at Method)
    ... 1 more
Java Result: 1

P.S 2 I managed to load the image with this code:

    private static final ImageView iv;    
        static {
            iv = new ImageView(StartPanel.class.getResource("/com/dx57dc/images/6.jpg").toExternalForm());

8/10/2013 7:20:39 PM

Accepted Answer

I got it to work. You not even need to handle the image load. The trick is to use the style property:

borderpane.setStyle("-fx-background-image: url(\"/images/system-help.png\");-fx-background-size: 500, 500;-fx-background-repeat: no-repeat;")

For sure you should outsource the css file and not use inline styles. It's just easier to show.

If you just want to set the background of the center area use this:

borderpane.getCenter().setStyle("-fx-background-image: url(\"/images/system-help.png\");-fx-background-size: 500, 500;-fx-background-repeat: no-repeat;")

The system-help.png is located in the /images directory, which is at the same level as the class file for the application (e.g. using eclipse it must be stored in the bin folder, which means you add a folder (for example call it resources) to your project and use it as a source folder, in this folder than you will need the image folder. Kinda weird but thats how fx needs it).

Edit: Resize as following:

private static final ImageView iv;    
static {
            iv = new ImageView(StartPanel.class.getResource("/com/dx57dc/images/6.jpg").toExternalForm());
            iv.resize(100, 100);
8/10/2013 7:24:58 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow