Bonjour à tous,
J'ai une Jlist qui contient des Jlabes dont chaque Jlabel contient à son tour un texte et une icône. Je voudrais faire un drag à partir de cette Jlist pour extraire le texte du Jlabel. Le seul problème est que le texte sélectionné est : "javax.swing.JLabel[,-145,-21,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@1d05c81,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=iconh_.png,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Etudiant,verticalAlignment=CENTER,verticalTextPosition=CENTER]"
Ceci est le code du mouseListener
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 MouseListener mouseListener = new MouseAdapter() { public void mouseClicked(MouseEvent e) { int index = jList2.locationToIndex(e.getPoint()); testlist2=true; final JLabel label = (JLabel)model.getElementAt(index); DragSource dragSource=DragSource.getDefaultDragSource(); final DragSourceListener dragListener=new DragSourceListener() { public void dragEnter(DragSourceDragEvent dsde) { doSourceDrag(dsde); } public void dragOver(DragSourceDragEvent dsde) { doSourceDrag(dsde); } public void dropActionChanged(DragSourceDragEvent dsde) { doSourceDrag(dsde); } public void dragExit(DragSourceEvent dse) { dse.getDragSourceContext().setCursor(DragSource.DefaultCopyDrop); } private void doSourceDrag(DragSourceDragEvent dsde) { DragSourceContext context = dsde.getDragSourceContext(); if ((dsde.getDropAction() & DnDConstants.ACTION_COPY)!=0) { context.setCursor(DragSource.DefaultCopyDrop); } else { context.setCursor(DragSource.DefaultCopyNoDrop); } } public void dragDropEnd(DragSourceDropEvent dsde) { if (!dsde.getDropSuccess()) return; if (dsde.getDropAction()==DnDConstants.ACTION_MOVE) { } } }; DragGestureListener gestureListener=new DragGestureListener() { public void dragGestureRecognized(DragGestureEvent dge) { Transferable transferable = new StringSelection(label.getText()); System.out.println(label.getText()); dge.startDrag(DragSource.DefaultCopyDrop,transferable, dragListener); } }; dragSource.createDefaultDragGestureRecognizer(label,DnDConstants.ACTION_COPY,gestureListener); } };
Je vous remercie énormément pour votre aide
Partager