Repository navigation
Expand file tree
/
Copy pathCopySerializable.java
More file actions
106 lines (101 loc) · 2.06 KB
/
Copy pathCopySerializable.java
File metadata and controls
106 lines (101 loc) · 2.06 KB
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
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
public class CopySerializable
{
Frame f = new Frame("copy object");
Button copy = new Button("copy");
Button paste = new Button("paste");
TextField name = new TextField(15);
TextField age = new TextField(15);
TextArea ta = new TextArea(3 , 30);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
public void init()
{
Panel p = new Panel();
p.add(new Label("name "));
p.add(name);
p.add(new Label("age"));
p.add(age);
f.add(p , BorderLayout.NORTH);
f.add(ta);
Panel bp = new Panel();
copy.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
copyPerson();
}
});
paste.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
readPerson();
}
catch (Exception ee)
{
ee.printStackTrace();
}
}
});
bp.add(copy);
bp.add(paste);
f.add(bp , BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
public void copyPerson()
{
Dog d = new Dog(name.getText() , Integer.parseInt(age.getText()));
SerialSelection ls = new SerialSelection(d);
clipboard.setContents(ls , null);
}
public void readPerson()throws Exception
{
DataFlavor peronFlavor = new DataFlavor("application/x-java-serialized-object;class=Dog");
if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor))
{
Dog d = (Dog)clipboard.getData(peronFlavor);
ta.setText(d.toString());
}
}
public static void main(String[] args)
{
new CopySerializable().init();
}
}
class Dog implements Serializable
{
private String name;
private int age;
public Dog(){}
public Dog(String name , int age)
{
this.name = name;
this.age = age;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return this.age;
}
public String toString()
{
return "DogObject[ name=" + name + " , age=" + age + " ]";
}
}