Personal greetings,
I was trying to do the Copy, Paste and Cut using an Array of integers in Java, but I found a lot of difficulty and little material (mainly in Portuguese).
Personal greetings,
I was trying to do the Copy, Paste and Cut using an Array of integers in Java, but I found a lot of difficulty and little material (mainly in Portuguese).
I was able to develop, here is the code below:
public class CopiarColarArray {
public void copiar(){
int[] data = new int[]{1,2,3,9872,5374,57};
MinhaClasseSelection selection = new MinhaClasseSelection(data);
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
cb.setContents(selection, selection);
}
public void recortar(){
int[] data = new int[]{1,2,3};
MinhaClasseSelection selection = new MinhaClasseSelection(data);
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
cb.setContents(selection, selection);
data = new int[]{};
}
public void colar(){
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
try {
Transferable t = cb.getContents(this);
if(t.isDataFlavorSupported(MinhaClasseSelection.ARRAY_INT_DATA_FLAVOR)){
int[] data = (int[])(t.getTransferData(MinhaClasseSelection.ARRAY_INT_DATA_FLAVOR));
for (int i = 0; i <data.length; i++) {
System.out.println("Array: "+data[i]);
}
}
} catch (UnsupportedFlavorException ex) {
Logger.getLogger(CopiarColarArray.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CopiarColarArray.class.getName()).log(Level.SEVERE, null, ex);
}
}
/* Minha classe perfonalizada do Selection */
public static class MinhaClasseSelection implements Transferable, ClipboardOwner {
private int[] intData;
public final static DataFlavor ARRAY_INT_DATA_FLAVOR = new DataFlavor(int[].class, "Array int");
public final static DataFlavor[] flavors ={ARRAY_INT_DATA_FLAVOR};
public MinhaClasseSelection(int[] data){
intData = data;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return flavors;
}
@Override
public boolean isDataFlavorSupported(DataFlavor df) {
for (int i = 0; i < flavors.length; i++) {
if (df.equals(flavors[i])){
return true;
}
}
return false;
}
@Override
public Object getTransferData(DataFlavor df) throws UnsupportedFlavorException, IOException {
if (df.equals(ARRAY_INT_DATA_FLAVOR)){
return intData;
}
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void lostOwnership(Clipboard clpbrd, Transferable t) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
}