Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    [JavaFX] ListView con immagini

    ciao!

    sto cercando di riempire una ListView in JavaFX con immagini.

    prendo la lista di immagini da un DirectoryChooser:
    codice:
    public class ListImages {
        public static ArrayList<String> getImages(File directory, Stage stage) {
            ArrayList<String> images = new ArrayList<>();
            DirectoryChooser dc = new DirectoryChooser();
            dc.setTitle("Open photo directory");
            dc.setInitialDirectory(directory);
            File dir = dc.showDialog(stage);
            if (dir != null) {
                File[] files = dir.listFiles((dir1, name) -> (name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".png")));
                for (File f : files) {
                    images.add(f.getAbsolutePath());
                }
            }
            return images;
        }
    }
    poi nel controller lancio questo metodo:
    codice:
    @FXML
    private void openDirectory() {
        images = ListImages.getImages(new File(System.getProperty("user.home") + "/1_TEST_PHOTO"), getStage());
        ObservableList<String> items = FXCollections.observableArrayList(images);
        listViewImages.setItems(items);
        listViewImages.setCellFactory(param -> new ListCell<String>() {
            private ImageView imageView = new ImageView();
    
            @Override
    public void updateItem(String name, boolean empty) {
                super.updateItem(name, empty);
                imageView.setImage(new Image(name));
                setGraphic(imageView);
    setText(name);
            }
        });
    }
    la lista viene correttamente riempita:
    codice:
    /home/matte/1_TEST_PHOTO/pexels-photo-23978.jpg
    /home/matte/1_TEST_PHOTO/pexels-photo-23978.jpg
    /home/matte/1_TEST_PHOTO/pexels-photo-28762.jpg
    /home/matte/1_TEST_PHOTO/pexels-photo.jpg
    /home/matte/1_TEST_PHOTO/pexels-photo-27414.jpg
    /home/matte/1_TEST_PHOTO/night-studio-color-minimalism-60470.jpeg
    /home/matte/1_TEST_PHOTO/training-train-lime-barbell-39688.jpeg
    /home/matte/1_TEST_PHOTO/pexels-photo.jpeg
    /home/matte/1_TEST_PHOTO/norway-mountain-sky-blue.jpg
    /home/matte/1_TEST_PHOTO/pexels-photo-70381.png
    /home/matte/1_TEST_PHOTO/fishing-sea-man-person.jpg
    /home/matte/1_TEST_PHOTO/pexels-photo-69224.jpeg
    però ottengo questo errore:
    codice:
    Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: URL must not be null
        at javafx.scene.image.Image.validateUrl(Image.java:1093)
        at javafx.scene.image.Image.<init>(Image.java:620)
        at com.mp.photoalbum.MainController$1.updateItem(MainController.java:47)
        at com.mp.photoalbum.MainController$1.updateItem(MainController.java:41)
        at javafx.scene.control.ListCell.updateItem(ListCell.java:480)
        at javafx.scene.control.ListCell.access$300(ListCell.java:72)
        at javafx.scene.control.ListCell$4.invalidated(ListCell.java:299)
        at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
        at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
        at javafx.scene.control.ListCell.setListView(ListCell.java:305)
        at javafx.scene.control.ListCell.updateListView(ListCell.java:494)
        at com.sun.javafx.scene.control.skin.ListViewSkin.createCell(ListViewSkin.java:292)
        at com.sun.javafx.scene.control.skin.ListViewSkin.lambda$new$2(ListViewSkin.java:99)
        at com.sun.javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1777)
        at com.sun.javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1879)
        at com.sun.javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2528)
        at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1189)
        at javafx.scene.Parent.layout(Parent.java:1087)
        at javafx.scene.Parent.layout(Parent.java:1093)
        at javafx.scene.Parent.layout(Parent.java:1093)
        at javafx.scene.Parent.layout(Parent.java:1093)
        at javafx.scene.Scene.doLayoutPass(Scene.java:552)
        at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
        at com.sun.javafx.tk.Toolkit.lambda$runPulse$2(Toolkit.java:398)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:397)
        at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:424)
        at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:518)
        at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:498)
        at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:491)
        at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:319)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
        at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)
        at java.lang.Thread.run(Thread.java:748)
    dove sbaglio secondo voi??

  2. #2
    risolto, da quanto ho capito ha bisogno di un URI.
    così funziona:
    codice:
    @FXML
    private void openDirectory() {
        images = ListImages.getImages(new File(System.getProperty("user.home") + "/1_TEST_PHOTO"), getStage());
        ObservableList<String> items = FXCollections.observableArrayList(images);
        listViewImages.setItems(items);
        listViewImages.setCellFactory(param -> new ListCell<String>() {
            private ImageView imageView = new ImageView();
    
            @Override
    public void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                if (empty || item == null) {
                    imageView.setImage(null);
                    setGraphic(null);
                    setText(null);
                } else {
                    imageView.setImage(new Image(new File(item).toURI().toString(), 0, 50, true, true));
                    setGraphic(imageView);
                    //setText(item);
    }
            }
        });
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.