FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Experiments/DragAndDrop.java at master · biblelamp/JavaExercises · GitHub
biblelamp
/
JavaExercises
Public
Notifications
You must be signed in to change notification settings
Fork
130
Star
153
Code
Issues
1
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaExercises
/
Experiments
/
DragAndDrop.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
68 lines (56 loc) · 1.94 KB
Breadcrumbs
JavaExercises
/
Experiments
/
DragAndDrop.java
Copy path
File metadata and controls
68 lines (56 loc) · 1.94 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**
* Simple drag and drop file example
* from
* http://java-demos.blogspot.ru/2013/06/drag-and-drop-file-in-jtextarea.html
* with small changes
*/
import
java
.
awt
.
dnd
.*;
import
java
.
awt
.*;
import
javax
.
swing
.*;
import
java
.
awt
.
datatransfer
.*;
import
java
.
io
.*;
class
DragAndDrop
extends
JFrame
{
private
JTextArea
jt
;
public
DragAndDrop
() {
createAndShowGUI
();
}
private
void
createAndShowGUI
() {
setTitle
(
"Drag and Drop Example"
);
setDefaultCloseOperation
(
EXIT_ON_CLOSE
);
setSize
(
400
,
400
);
setLocationRelativeTo
(
null
);
// to the center of the screen
setLayout
(
new
BorderLayout
());
// Create JTextArea
jt
=
new
JTextArea
();
JScrollPane
sb
=
new
JScrollPane
(
jt
);
add
(
sb
);
enableDragAndDrop
();
setVisible
(
true
);
}
private
void
enableDragAndDrop
() {
DropTarget
target
=
new
DropTarget
(
jt
,
new
DropTargetListener
() {
public
void
dragEnter
(
DropTargetDragEvent
e
) {
}
public
void
dragExit
(
DropTargetEvent
e
) {
}
public
void
dragOver
(
DropTargetDragEvent
e
) {
}
public
void
dropActionChanged
(
DropTargetDragEvent
e
) {
}
public
void
drop
(
DropTargetDropEvent
e
) {
try
{
// Accept the drop first, important!
e
.
acceptDrop
(
DnDConstants
.
ACTION_COPY_OR_MOVE
);
// Get the files that are dropped as java.util.List
java
.
util
.
List
list
= (
java
.
util
.
List
)
e
.
getTransferable
().
getTransferData
(
DataFlavor
.
javaFileListFlavor
);
// Now get the first file from the list,
File
file
= (
File
)
list
.
get
(
0
);
jt
.
read
(
new
FileReader
(
file
),
null
);
}
catch
(
Exception
ex
) {}
}
});
}
static
public
void
main
(
String
args
[]) {
new
DragAndDrop
();
}
}
Back
|
FazBrowse Home
|
New Git URL