-1

I get the same error again and again when i try to add images to my JavaFX. Error: at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) (more lines follow). It must be related to the path to the images that I specified. I have already read through the general "path" tutorial on StackOverflow without success. I just want to make a simple scrollBar which enables scrolling through some Images i added to a VBox.

Heres my directory:

enter image description here

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Shadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Scrollbar3 extends Application {

    // Variablen
    final ScrollBar scrollbar = new ScrollBar();
    final String[] images = {
            "Bilder/bild0.jpg", // 0
            "Bilder/bild1.jpg",
            "Bilder/bild2.jpg",
            "Bilder/bild3.jpg",
            "Bilder/bild4.jpg",
    };
    
    DropShadow shadow = new DropShadow();
    final VBox vbox = new VBox();

    @Override
    public void start(Stage primaryStage) throws Exception {

        // Scene / root
        Group root = new Group();
        Scene scene = new Scene(root, 400, 400);
        root.getChildren().addAll(vbox, scrollbar);
        

        // Effekt
        shadow.setColor(Color.BLACK);
        shadow.setOffsetX(10);
        shadow.setOffsetY(10);

        // VBox
        vbox.setLayoutX(5);
        vbox.setSpacing(10);
        vbox.setPadding(new Insets(20));

        // Scrollbar
        scrollbar.setLayoutX(scene.getWidth() - scrollbar.getWidth());
        scrollbar.setOrientation(Orientation.VERTICAL);
        scrollbar.setPrefHeight(400);
        scrollbar.setMax(2000);

        // Bilder
        for(int i = 0; i < images.length; i++) {
            final ImageView imageView = new ImageView(new Image(images[i]));
            imageView.setEffect(shadow);
            vbox.getChildren().add(imageView);
        }

        // Eventhanlding / Listener
        scrollbar.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
                vbox.setLayoutY(-newValue.doubleValue());
                
            }
        });

        // Stage
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}

enter image description here

Encera
  • 45
  • 5
  • Provide the full stack-trace! – mipa Apr 06 '22 at 10:58
  • StackTraceProvided – Encera Apr 06 '22 at 11:16
  • 1
    Don't post text as images. Post the stack trace as text, formatted as code. You can do the same with the project layout, using a command such as `tree`. – James_D Apr 06 '22 at 11:49
  • Anyway, I strongly recommend you do not use the default package. I am not actually sure how relative resource names are resolved from a class in the default package. You should also stick to standard naming conventions (package names should be all lower-case). You can try using an absolute resource name (so `/bilder/bild0.jpg`, if the package is renamed appropriately; but again, I recommend packaging your application properly. – James_D Apr 06 '22 at 11:52
  • See also https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other and https://edencoding.com/where-to-put-resource-files-in-javafx/ for more details. – James_D Apr 06 '22 at 11:54

1 Answers1

0

It looks like you are treating your images as resource images because they are contained in the source folder.

Change the line with the image creation to

final ImageView imageView = new ImageView(new Image(this.getClass().getResourceAsStream(images[i])));

and add an "/" in front of your image paths.

mipa
  • 10,369
  • 2
  • 16
  • 35