Bonjour,

J'aimerais pouvoir faire du grad and drop entre ma sélection d'une JList et un JTextArea mais en allant à la ligne entre chaque drop.

Cad que si par exemple, je lui droppe un élémént "toto", puis "titi", que sur mon JTextArea, qu'il y ait "toto" puis retour chariot, puis "titi".

Pour le moment, ça ne marche pas, il me colle chaque contenu de texte à la suite du précédent :-(

Voici le TransferHandler personnalisé que j'utilise, associé à mon JTextArea:

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
public class ToTransferHandler2 extends javax.swing.TransferHandler {
 
 
    private static final long serialVersionUID = 1L;
    int action;
 
    public ToTransferHandler2(int action) {
        this.action = action;
    }
 
    public boolean canImport(TransferHandler.TransferSupport support) {
 
        // for the demo, we'll only support drops (not clipboard paste)
        if (!support.isDrop()) {
            return false;
        }
 
        // we only import Strings
        if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            return false;
        }
 
        boolean actionSupported = (action & support.getSourceDropActions()) == action;
        if (actionSupported) {
            support.setDropAction(action);
            return true;
        }
        return false;
    }
 
    public boolean importData(TransferHandler.TransferSupport support) {
 
        int offsetLineEnd;
        int nbreLignes;
 
        // if we can't handle the import, say so
        if (!canImport(support)) {
            return false;
        }
 
        // fetch the drop location
        //JTextArea.DropLocation dl = (JTextArea.DropLocation)support.getDropLocation();
        // int index = dl.getIndex();
 
        // fetch the data and bail if this fails
        String data;
        try {
            data = (String)support.getTransferable().getTransferData(DataFlavor.stringFlavor);
        } catch (UnsupportedFlavorException e) {
            return false;
        } catch (java.io.IOException e) {
            return false;
        }
 
        JTextArea jtextArea = (JTextArea)support.getComponent();
        nbreLignes = jtextArea.getLineCount();
 
        try {
            offsetLineEnd = jtextArea.getLineEndOffset(nbreLignes);
            jtextArea.insert("\n", offsetLineEnd);
            jtextArea.append(data);
        } catch (BadLocationException e) {
            // Log the error
        }
        return true;
    }
 
}
Pour que ca marche, j'essaie de :
- récupérer le nombre de lignes total de mon area
- récupérer l'offset correspondante au numéro de la dernière ligne
- écrire un \n à cet offset
- concaténer mon texte derrière

Mais ca ne marche pas

Le plus bizarre c'est que quand j'essaie de débugger cela, juste après qu'il se trouve sur la ligne "offsetLineEnd = jtextArea.getLineEndOffset(nbreLignes);", il va directement ensuite sur "return true;" :-(

Pour info, voici aussi le TransferHandler associé à ma JList (dont on drag une sélection):

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
public class FromTransferHandler extends TransferHandler {
 
    private static final long serialVersionUID = 1L;
 
    private int index = 0;
 
    private JList dragFrom;
    private DefaultListModel from;
 
    public FromTransferHandler(JList dragFrom){
        super();
        this.dragFrom = dragFrom;
        this.from = (DefaultListModel)dragFrom.getModel();
    }
 
    public int getSourceActions(JComponent comp) {
        return COPY_OR_MOVE;
    }
 
    public Transferable createTransferable(JComponent comp) {
        index = dragFrom.getSelectedIndex();
        // Voir par la suite, j'ai enlevé un bout de condition 
        if (index < 0) {
            return null;
        }
        return new StringSelection((String)dragFrom.getSelectedValue());
    }
 
  /*  public void exportDone(JComponent comp, Transferable trans, int action) {
        if (action != MOVE) {
            return;
        }
        from.removeElementAt(index);
    }*/
 
}
Je vous remercie :-)

PS: J'ai essayé ceci:

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
public boolean importData(TransferHandler.TransferSupport support) {
 
        int offsetLineEnd;
        int nbreLignes;
 
        // if we can't handle the import, say so
        if (!canImport(support)) {
            return false;
        }
 
        // fetch the data and bail if this fails
        String data;
        try {
            data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
        } catch (UnsupportedFlavorException e) {
            return false;
        } catch (java.io.IOException e) {
            return false;
        }
 
        JTextArea jtextArea = (JTextArea) support.getComponent();
 
        jtextArea.append("\n" + data);
 
        return true;
    }
mais le souci est que forcément, il va m'insérer un retour chariot à chaque drop, alors que pour le premier drop, il ne faut pas en insérer...