IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaFX Discussion :

ListView et Boutons basé sur le type "File"


Sujet :

JavaFX

  1. #1
    Membre du Club

    Homme Profil pro
    Hobbyiste
    Inscrit en
    Juillet 2018
    Messages
    126
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Hobbyiste

    Informations forums :
    Inscription : Juillet 2018
    Messages : 126
    Points : 68
    Points
    68
    Billets dans le blog
    1
    Par défaut ListView et Boutons basé sur le type "File"
    Bonjour,


    Je n'y connais rien en Drag Drop et je voudrais des informations.

    J'ai une ListView qui contient les fichiers chargés par l'utilisateur ou le programme au lancement.

    Les comportements que je voudrais:
    - Réordonner les fichiers (un ou plusieurs)
    - Effacer les fichiers (tout -fait, par sélection
    - Ajouter des fichiers (fait par un FileChooser)

    Draguer des fichiers vers les boutons de la boîte à rythme (un ou plusieurs)

    Sur la barre de temps de boucle, pouvoir effacer des fichiers.

    Pour le moment je cale là-dessus.

  2. #2
    Membre du Club

    Homme Profil pro
    Hobbyiste
    Inscrit en
    Juillet 2018
    Messages
    126
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Hobbyiste

    Informations forums :
    Inscription : Juillet 2018
    Messages : 126
    Points : 68
    Points
    68
    Billets dans le blog
    1
    Par défaut Jusque maintenant
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    package be.manudahmen.mylittlesynth.rythms;
     
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.control.ContentDisplay;
    import javafx.scene.control.ListCell;
    import javafx.scene.control.ListView;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.input.ClipboardContent;
    import javafx.scene.input.DragEvent;
    import javafx.scene.input.Dragboard;
    import javafx.scene.input.TransferMode;
    import javafx.scene.layout.VBox;
     
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
     
    /**
     * Created by Win on 14-08-18.
     */
    class BirdCell extends ListCell<File> {
        private static final ObservableList<Image> birdImages = FXCollections.observableArrayList();
        private final ImageView imageView = new ImageView();
        private File file
                ;
        public BirdCell() {
            ListCell thisCell = this;
            ListView<File> birdList = new ListView<>();
            birdList.setCellFactory(param -> new BirdCell());
            birdList.setPrefWidth(180);
     
            VBox layout = new VBox(birdList);
            layout.setPadding(new Insets(10));
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
            setAlignment(Pos.CENTER);
     
            setOnDragDetected(event -> {
                if (getItem() == null) {
                    return;
                }
     
                ObservableList<File> items = getListView().getItems();
     
                Dragboard dragboard = startDragAndDrop(TransferMode.MOVE);
                ClipboardContent content = new ClipboardContent();
                content.putFiles(Arrays.asList(getItem().listFiles()));
                dragboard.setDragView(
                        birdImages.get(
                                items.indexOf(
                                        getItem()
                                )
                        )
                );
                dragboard.setContent(content);
     
                event.consume();
            });
     
            setOnDragOver(event -> {
                if (event.getGestureSource() != thisCell &&
                        event.getDragboard().hasString()) {
                    event.acceptTransferModes(TransferMode.MOVE);
                }
     
                event.consume();
            });
     
            setOnDragEntered(event -> {
                if (event.getGestureSource() != thisCell &&
                        event.getDragboard().hasString()) {
                    setOpacity(0.3);
                }
            });
     
            setOnDragExited(event -> {
                if (event.getGestureSource() != thisCell &&
                        event.getDragboard().hasString()) {
                    setOpacity(1);
                }
            });
     
            setOnDragDropped(event -> {
                if (getItem() == null) {
                    return;
                }
     
                Dragboard db = event.getDragboard();
                boolean success = false;
     
                if (db.hasString()) {
                    ObservableList<File> items = getListView().getItems();
                    int draggedIdx = items.indexOf(db.getString());
                    int thisIdx = items.indexOf(getItem());
     
                    Image temp = birdImages.get(draggedIdx);
                    birdImages.set(draggedIdx, birdImages.get(thisIdx));
                    birdImages.set(thisIdx, temp);
     
                    items.set(draggedIdx, getItem());
                    items.set(thisIdx, db.getFiles().get(0));
     
                    List<File> itemscopy = new ArrayList<>(getListView().getItems());
                    getListView().getItems().setAll(itemscopy);
     
                    success = true;
                }
                event.setDropCompleted(success);
     
                event.consume();
            });
     
            setOnDragDone(DragEvent::consume);
        }
     
        @Override
        protected void updateItem(File item, boolean empty) {
            super.updateItem(item, empty);
     
            if (empty || item == null) {
                setGraphic(null);
            } else {
                file = item;
            }
        }
     
        public File getFile() {
            return file;
        }
     
     
    }

  3. #3
    Membre du Club

    Homme Profil pro
    Hobbyiste
    Inscrit en
    Juillet 2018
    Messages
    126
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Hobbyiste

    Informations forums :
    Inscription : Juillet 2018
    Messages : 126
    Points : 68
    Points
    68
    Billets dans le blog
    1
    Par défaut Ce que j'obtiens au démarrage
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    Exception in Application start method
    java.lang.reflect.InvocationTargetException
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:498)
    	at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    	at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:498)
    	at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
    Caused by: java.lang.RuntimeException: Exception in Application start method
    	at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    	at java.lang.Thread.run(Thread.java:748)
    Caused by: java.lang.NullPointerException
    	at com.sun.javafx.scene.control.skin.ListCellSkin.<init>(ListCellSkin.java:42)
    	at javafx.scene.control.ListCell.createDefaultSkin(ListCell.java:338)
    	at javafx.scene.control.Control.impl_processCSS(Control.java:872)
    	at javafx.scene.Node.processCSS(Node.java:9058)
    	at javafx.scene.Node.applyCss(Node.java:9155)
    	at javafx.scene.Node.impl_processCSS(Node.java:9069)
    	at com.sun.javafx.scene.control.skin.LabeledSkinBase.updateChildren(LabeledSkinBase.java:586)
    	at com.sun.javafx.scene.control.skin.LabeledSkinBase.<init>(LabeledSkinBase.java:127)
    	at com.sun.javafx.scene.control.skin.CellSkinBase.<init>(CellSkinBase.java:111)
    	at com.sun.javafx.scene.control.skin.ListCellSkin.<init>(ListCellSkin.java:40)
    	at javafx.scene.control.ListCell.createDefaultSkin(ListCell.java:338)
    	at javafx.scene.control.Control.impl_processCSS(Control.java:872)
    	at javafx.scene.Node.processCSS(Node.java:9058)
    	at javafx.scene.Node.applyCss(Node.java:9155)
    	at com.sun.javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1964)
    	at com.sun.javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1797)
    	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.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.preferredSize(Scene.java:1646)
    	at javafx.scene.Scene.impl_preferredSize(Scene.java:1720)
    	at javafx.stage.Window$9.invalidated(Window.java:864)
    	at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109)
    	at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)
    	at javafx.stage.Window.setShowing(Window.java:940)
    	at javafx.stage.Window.show(Window.java:955)
    	at javafx.stage.Stage.show(Stage.java:259)
    	at be.manudahmen.mylittlesynth.App.start(App.java:312)
    	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    	at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    	at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    	at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    	at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    	at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    	... 1 more
    Exception running application be.manudahmen.mylittlesynth.App

  4. #4
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 840
    Points : 22 854
    Points
    22 854
    Billets dans le blog
    51
    Par défaut
    Peut-etre lie au fait d'avoir une liste dans la cellule, etc...

    Enfin bref en nettoyant un peu le code (qui semble directement recopie d'une question sur StackOverflow) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    package listfile;
     
    import java.io.File;
    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.ListView;
    import javafx.scene.input.DragEvent;
    import javafx.scene.input.Dragboard;
    import javafx.scene.input.TransferMode;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
     
    public class Main extends Application {
     
        @Override
        public void start(Stage primaryStage) throws Exception {
            final ListView<File> birdList = new ListView();
            birdList.setPrefWidth(180);
            birdList.setCellFactory(listView -> new BirdCell());
            // Permet de lacher des fichiers dans la liste quand elle est vide.
            birdList.setOnDragOver(event -> {
                event.acceptTransferModes(TransferMode.MOVE);event.consume();
                event.consume();
            });
            birdList.setOnDragDropped(event -> {
                final Dragboard db = event.getDragboard();
                boolean success = false;
                if (db.hasFiles() && birdList.getItems().isEmpty()) {
                    birdList.getItems().addAll(db.getFiles());
                }
                event.setDropCompleted(success);
                event.consume();
            });
            birdList.setOnDragDone(DragEvent::consume);
            final StackPane root = new StackPane(birdList);
            root.setPadding(new Insets(10));
            final Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.setWidth(400);
            primaryStage.setHeight(600);
            primaryStage.show();
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    package listfile;
     
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.WeakHashMap;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.collections.ObservableList;
    import javafx.geometry.Pos;
    import javafx.scene.Node;
    import javafx.scene.control.ContentDisplay;
    import javafx.scene.control.ListCell;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.input.ClipboardContent;
    import javafx.scene.input.DragEvent;
    import javafx.scene.input.Dragboard;
    import javafx.scene.input.TransferMode;
     
    public class BirdCell extends ListCell<File> {
     
        private static final Map<File, Image> IMAGE_CACHE_MAP = new WeakHashMap<>();
        private final ImageView imageView = new ImageView();
        private File file;
     
        public BirdCell() {
            imageView.setFitWidth(100);
            imageView.setFitHeight(100);
            imageView.setPreserveRatio(true);
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
            setAlignment(Pos.CENTER);
            setOnDragDetected(event -> {
                if (getItem() == null) {
                    return;
                }
                Dragboard dragboard = startDragAndDrop(TransferMode.MOVE);
                ClipboardContent content = new ClipboardContent();
                content.putFiles(Arrays.asList(getItem()));
                final Image source = IMAGE_CACHE_MAP.get(getItem());
    //            content.putImage(source);
                if (source != null) {
                    final ImageView thumbnail = new ImageView(source);
                    thumbnail.setFitWidth(75);
                    thumbnail.setFitHeight(75);
                    thumbnail.setPreserveRatio(true);
                    final Image preview = thumbnail.snapshot(null, null);
                    dragboard.setDragView(preview);
                }
                dragboard.setContent(content);
                event.consume();
            });
            setOnDragOver(event -> {
                if (event.getGestureSource() != BirdCell.this && event.getDragboard().hasFiles()) {
                    event.acceptTransferModes(TransferMode.MOVE);
                }
                event.consume();
            });
            setOnDragEntered(event -> {
                if (event.getGestureSource() != BirdCell.this && event.getDragboard().hasFiles()) {
                    setOpacity(0.3);
                }
            });
            setOnDragExited(event -> {
                if (event.getGestureSource() != BirdCell.this && event.getDragboard().hasFiles()) {
                    setOpacity(1);
                }
            });
            setOnDragDropped(event -> {
                Dragboard db = event.getDragboard();
                boolean success = false;
                if (db.hasFiles()) {
                    if (!isEmpty()) {
                        ObservableList<File> items = getListView().getItems();
                        int draggedIdx = items.indexOf(db.getFiles().get(0));
                        int thisIdx = items.indexOf(getItem());
                        List<File> itemscopy = new ArrayList<>(getListView().getItems());
                        itemscopy.set(draggedIdx, items.get(thisIdx));
                        itemscopy.set(thisIdx, items.get(draggedIdx));
                        getListView().getItems().setAll(itemscopy);
                        success = true;
                    } else {
                        // Permet de rajouter des fichiers dans la partie vide de la liste.
                        db.getFiles().stream()
                                .filter(file -> !getListView().getItems().contains(file))
                                .forEach(getListView().getItems()::add);
                    }
                }
                event.setDropCompleted(success);
                event.consume();
            });
            setOnDragDone(DragEvent::consume);
        }
     
        @Override
        protected void updateItem(File item, boolean empty) {
            super.updateItem(item, empty);
            imageView.setImage(null);
            String text = null;
            Node graphic = null;
            if (!empty && item != null) {
                text = item.getAbsolutePath();
                try {
                    Image image = IMAGE_CACHE_MAP.get(item);
                    if (image == null) {
                        image = new Image(item.toURI().toURL().toExternalForm());
                        IMAGE_CACHE_MAP.put(item, image);
                    }
                    imageView.setImage(image);
                    graphic = imageView;
                } catch (Exception ex) {
                    Logger.getLogger(BirdCell.class.getName()).log(Level.WARNING, null, ex);
                }
            }
            setText(text);
            setGraphic(graphic);
        }
    }
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

Discussions similaires

  1. [SP-2013] Type de document basé sur un type existant, supprimer des colonnes ?
    Par SpaceFrog dans le forum SharePoint
    Réponses: 9
    Dernier message: 18/12/2014, 14h05

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo