Skip to content

Commit 906fea0

Browse files
committed
TestAWT : add clipboard tests
1 parent 7a13e09 commit 906fea0

2 files changed

Lines changed: 129 additions & 40 deletions

File tree

‎src/javaforce/tests/TestAWT.form‎

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@
3131
<EmptySpace max="-2" attributes="0"/>
3232
<Component id="jScrollPane1" max="32767" attributes="0"/>
3333
</Group>
34-
<Group type="102" attributes="0">
35-
<Component id="textField1" pref="421" max="32767" attributes="0"/>
36-
<EmptySpace max="-2" attributes="0"/>
37-
<Component id="button1" min="-2" max="-2" attributes="0"/>
38-
</Group>
34+
<Component id="textField" pref="450" max="32767" attributes="0"/>
35+
</Group>
36+
<EmptySpace max="-2" attributes="0"/>
37+
<Group type="103" groupAlignment="0" max="-2" attributes="0">
38+
<Component id="copy" alignment="1" max="32767" attributes="0"/>
39+
<Component id="button1" alignment="0" max="32767" attributes="0"/>
40+
<Component id="cut" alignment="1" max="32767" attributes="0"/>
41+
<Component id="paste" alignment="1" max="32767" attributes="0"/>
3942
</Group>
4043
<EmptySpace max="-2" attributes="0"/>
4144
</Group>
@@ -46,13 +49,20 @@
4649
<Group type="102" alignment="0" attributes="0">
4750
<EmptySpace max="-2" attributes="0"/>
4851
<Group type="103" groupAlignment="0" max="-2" attributes="0">
49-
<Component id="textField1" max="32767" attributes="0"/>
52+
<Component id="textField" max="32767" attributes="0"/>
5053
<Component id="button1" max="32767" attributes="0"/>
5154
</Group>
5255
<EmptySpace max="-2" attributes="0"/>
53-
<Group type="103" groupAlignment="0" attributes="0">
54-
<Component id="drag_me" min="-2" max="-2" attributes="0"/>
55-
<Component id="jScrollPane1" min="-2" max="-2" attributes="0"/>
56+
<Group type="103" groupAlignment="0" max="-2" attributes="0">
57+
<Group type="102" alignment="0" attributes="0">
58+
<Component id="copy" min="-2" max="-2" attributes="0"/>
59+
<EmptySpace max="-2" attributes="0"/>
60+
<Component id="cut" min="-2" max="-2" attributes="0"/>
61+
<EmptySpace max="-2" attributes="0"/>
62+
<Component id="paste" min="-2" max="-2" attributes="0"/>
63+
</Group>
64+
<Component id="jScrollPane1" alignment="0" pref="0" max="32767" attributes="0"/>
65+
<Component id="drag_me" alignment="0" max="32767" attributes="0"/>
5666
</Group>
5767
<EmptySpace max="32767" attributes="0"/>
5868
</Group>
@@ -62,19 +72,19 @@
6272
<SubComponents>
6373
<Component class="java.awt.Button" name="button1">
6474
<Properties>
65-
<Property name="label" type="java.lang.String" value="button1"/>
75+
<Property name="label" type="java.lang.String" value="Button"/>
6676
</Properties>
6777
<Events>
6878
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="button1ActionPerformed"/>
6979
</Events>
7080
</Component>
71-
<Component class="java.awt.TextField" name="textField1">
72-
<Properties>
73-
<Property name="text" type="java.lang.String" value="textField1"/>
74-
</Properties>
81+
<Component class="java.awt.TextField" name="textField">
7582
</Component>
7683
<Component class="javax.swing.JLabel" name="drag_me">
7784
<Properties>
85+
<Property name="background" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor">
86+
<Color blue="ff" green="33" red="0" type="rgb"/>
87+
</Property>
7888
<Property name="text" type="java.lang.String" value="Drag Me"/>
7989
</Properties>
8090
</Component>
@@ -85,7 +95,7 @@
8595

8696
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
8797
<SubComponents>
88-
<Component class="javax.swing.JTextArea" name="drop_area">
98+
<Component class="javax.swing.JTextArea" name="text_area">
8999
<Properties>
90100
<Property name="columns" type="int" value="20"/>
91101
<Property name="rows" type="int" value="5"/>
@@ -94,5 +104,29 @@
94104
</Component>
95105
</SubComponents>
96106
</Container>
107+
<Component class="javax.swing.JButton" name="copy">
108+
<Properties>
109+
<Property name="text" type="java.lang.String" value="Copy"/>
110+
</Properties>
111+
<Events>
112+
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="copyActionPerformed"/>
113+
</Events>
114+
</Component>
115+
<Component class="javax.swing.JButton" name="paste">
116+
<Properties>
117+
<Property name="text" type="java.lang.String" value="Paste"/>
118+
</Properties>
119+
<Events>
120+
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="pasteActionPerformed"/>
121+
</Events>
122+
</Component>
123+
<Component class="javax.swing.JButton" name="cut">
124+
<Properties>
125+
<Property name="text" type="java.lang.String" value="Cut"/>
126+
</Properties>
127+
<Events>
128+
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="cutActionPerformed"/>
129+
</Events>
130+
</Component>
97131
</SubComponents>
98132
</Form>

‎src/javaforce/tests/TestAWT.java‎

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ public TestAWT() {
2525
e.startDrag(DragSource.DefaultCopyDrop, transferable, null);
2626
}
2727
);
28-
new DropTarget(drop_area, DnDConstants.ACTION_COPY_OR_MOVE,
28+
new DropTarget(text_area, DnDConstants.ACTION_COPY_OR_MOVE,
2929
new DropTargetAdapter() {
3030
public void drop(DropTargetDropEvent evt) {
3131
try {
3232
evt.acceptDrop(DnDConstants.ACTION_COPY);
3333
Transferable tr = evt.getTransferable();
3434
if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) {
3535
String data = (String) tr.getTransferData(DataFlavor.stringFlavor);
36-
drop_area.setText(data);
36+
text_area.setText(data);
3737
evt.dropComplete(true);
3838
} else {
3939
evt.rejectDrop();
@@ -54,28 +54,39 @@ public void drop(DropTargetDropEvent evt) {
5454
private void initComponents() {
5555

5656
button1 = new java.awt.Button();
57-
textField1 = new java.awt.TextField();
57+
textField = new java.awt.TextField();
5858
drag_me = new javax.swing.JLabel();
5959
jScrollPane1 = new javax.swing.JScrollPane();
60-
drop_area = new javax.swing.JTextArea();
60+
text_area = new javax.swing.JTextArea();
61+
copy = new javax.swing.JButton();
62+
paste = new javax.swing.JButton();
63+
cut = new javax.swing.JButton();
6164

6265
addWindowListener(new java.awt.event.WindowAdapter() {
6366
public void windowClosing(java.awt.event.WindowEvent evt) {
6467
exitForm(evt);
6568
}
6669
});
6770

68-
button1.setLabel("button1");
71+
button1.setLabel("Button");
6972
button1.addActionListener(this::button1ActionPerformed);
7073

71-
textField1.setText("textField1");
72-
74+
drag_me.setBackground(new java.awt.Color(0, 51, 255));
7375
drag_me.setText("Drag Me");
7476

75-
drop_area.setColumns(20);
76-
drop_area.setRows(5);
77-
drop_area.setText("Drop Here");
78-
jScrollPane1.setViewportView(drop_area);
77+
text_area.setColumns(20);
78+
text_area.setRows(5);
79+
text_area.setText("Drop Here");
80+
jScrollPane1.setViewportView(text_area);
81+
82+
copy.setText("Copy");
83+
copy.addActionListener(this::copyActionPerformed);
84+
85+
paste.setText("Paste");
86+
paste.addActionListener(this::pasteActionPerformed);
87+
88+
cut.setText("Cut");
89+
cut.addActionListener(this::cutActionPerformed);
7990

8091
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
8192
this.setLayout(layout);
@@ -88,23 +99,32 @@ public void windowClosing(java.awt.event.WindowEvent evt) {
8899
.addComponent(drag_me)
89100
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
90101
.addComponent(jScrollPane1))
91-
.addGroup(layout.createSequentialGroup()
92-
.addComponent(textField1, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
93-
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
94-
.addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
102+
.addComponent(textField, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE))
103+
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
104+
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
105+
.addComponent(copy, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
106+
.addComponent(button1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
107+
.addComponent(cut, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
108+
.addComponent(paste, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
95109
.addContainerGap())
96110
);
97111
layout.setVerticalGroup(
98112
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
99113
.addGroup(layout.createSequentialGroup()
100114
.addContainerGap()
101115
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
102-
.addComponent(textField1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
116+
.addComponent(textField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
103117
.addComponent(button1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
104118
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
105-
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
106-
.addComponent(drag_me)
107-
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
119+
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
120+
.addGroup(layout.createSequentialGroup()
121+
.addComponent(copy)
122+
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
123+
.addComponent(cut)
124+
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
125+
.addComponent(paste))
126+
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
127+
.addComponent(drag_me, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
108128
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
109129
);
110130

@@ -114,14 +134,26 @@ public void windowClosing(java.awt.event.WindowEvent evt) {
114134
/**
115135
* Exit the Application
116136
*/
117-
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
118-
System.exit(0);
119-
}//GEN-LAST:event_exitForm
137+
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
138+
System.exit(0);
139+
}//GEN-LAST:event_exitForm
120140

121141
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
122142
test();
123143
}//GEN-LAST:event_button1ActionPerformed
124144

145+
private void copyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyActionPerformed
146+
cb_copy();
147+
}//GEN-LAST:event_copyActionPerformed
148+
149+
private void cutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cutActionPerformed
150+
cb_cut();
151+
}//GEN-LAST:event_cutActionPerformed
152+
153+
private void pasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pasteActionPerformed
154+
cb_paste();
155+
}//GEN-LAST:event_pasteActionPerformed
156+
125157
/**
126158
* @param args the command line arguments
127159
*/
@@ -136,13 +168,36 @@ public void run() {
136168

137169
// Variables declaration - do not modify//GEN-BEGIN:variables
138170
private java.awt.Button button1;
171+
private javax.swing.JButton copy;
172+
private javax.swing.JButton cut;
139173
private javax.swing.JLabel drag_me;
140-
private javax.swing.JTextArea drop_area;
141174
private javax.swing.JScrollPane jScrollPane1;
142-
private java.awt.TextField textField1;
175+
private javax.swing.JButton paste;
176+
private java.awt.TextField textField;
177+
private javax.swing.JTextArea text_area;
143178
// End of variables declaration//GEN-END:variables
144179

145180
private void test() {
146-
System.out.println("button clicked!");
181+
System.out.println("Button clicked!");
182+
textField.setText("Button clicked!");
183+
}
184+
185+
JFClipboard cb = new JFClipboard();
186+
187+
private void cb_copy() {
188+
JFClipboard.writeString(text_area.getText());
147189
}
190+
191+
private void cb_cut() {
192+
JFClipboard.writeString(text_area.getText());
193+
text_area.setText("");
194+
}
195+
196+
private void cb_paste() {
197+
String str = JFClipboard.readString();
198+
if (str == null) return;
199+
text_area.setText(str);
200+
}
201+
202+
148203
}

0 commit comments

Comments
 (0)