Copy, Paste, and Crop an Array of Java Integers using Clipboard?

Asked

Viewed 132 times

3

Personal greetings,

I was trying to perform the processes of Copy, Paste and Cut using an Array of integers in Java, but I found a lot of difficulty and little material (mainly in Portuguese).

  • 2

    Nice of you to take the initiative, but this is a Q and A site, not a forum. You can ask a question, and posting your solution as an answer will be a legal contribution. See here a little more about how the site works: [Tour]. Also read the Community FAQ, which has many useful guidelines. Remember that you can [Edit] the question by copying its contents to a new answer, leaving in the initial post only the question of how to do what your post proposes. So it will stay in the proper format and help other people.

  • Thanks for the observation, first post... you know how it is. rsrsrs

  • 2

    Now it’s cool. Even, then you can click on Green on the side of your answer, to mark as accepted solution. If you like, you can then elaborate on the question with the possible difficulties you had before you got there. But the fundamental step, you already have. And welcome. Then take a look at [meta], which is our main "site that discusses the site". While here we ask questions and answers about programming, there we discuss how the site works, and make possible suggestions for improvements and corrections.

1 answer

3


I managed to develop, follows below the code:

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.
    }

  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.