Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When i copy a texts (separated by tab), my program split the copied texts and store in the array of string. when i press the button it will display the array of text one by one and display along with set the text to clipboard. i am able to display text but not able to set in the clipboard. Can any one correct my code. my code is given below. thanks in advance.
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.event.*;
import javax.swing.*;

class MyProj {
public static void main(String [] args) throws Exception {

	Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
	String result = (String) clipboard.getData(DataFlavor.stringFlavor);
	
	StringSelection stringSelection = new StringSelection (aa[i]);
	clipboard.setContents (stringSelection, null);
	
    Buttonpanel panela = new Buttonpanel();
    panela.getstring(result);

    JFrame Frame = new JFrame("hiiiiii");
    Buttonpanel panel = new Buttonpanel();
    
    Frame.add(panel);
    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Frame.setBounds(700,300,300,200);
    Frame.setVisible(true);
	}
}

class Buttonpanel extends JPanel implements ActionListener {

JButton button;
JLabel label;

static String[] aa ;
String bb = "";
static int i = 0;

Buttonpanel()
{
    button = new JButton("Push");
    add(button);
    button.addActionListener(this);
    label = new JLabel("hiiii");
    add(label);
}

void getstring(String bb)
{
	aa = bb.split("	");	
}

public void actionPerformed(ActionEvent e)
{
    label.setText(aa[i]);
   System.out.println(""+ i );  //loop working here
	i ++;
}
}


What I have tried:

i am able to display text but not able to set in the clipboard. Can any one correct my code. my code is given below. thanks in advance.
Posted
Updated 29-Jan-17 2:17am
v2
Comments
Afzaal Ahmad Zeeshan 29-Jan-17 5:26am    
Can you access the clipboard, or what does the clipboard return when you try to access it?
Member 12972343 29-Jan-17 13:55pm    
yes i am able to access clipboard if i put directly any string insted of array value aa[i].

1 solution

That's because you only set one string into the clipboard in your main method thus:
Java
StringSelection stringSelection = new StringSelection (aa[i]);
clipboard.setContents (stringSelection, null);

You need to wait until you have collected all the text in the array, and then convert it into a form that you can send to the clipboard.
 
Share this answer
 
Comments
Member 12972343 29-Jan-17 12:46pm    
@Richard MacCutchan when i will press button, value of "i" will increament by 1 each time and the value of string will also change. no need to wait because all text already splited and stored in array. i just want that when i click button, the value of array set to clipboard, when i click next times, the second value of array will set to clipboad and so on.
Richard MacCutchan 30-Jan-17 4:06am    
So you need to add some code to your button click method to put the latest string in the ClipBoard. However, this will still mean that there will only be a single string in the ClipBoard at any time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900