Napisałem program (prosty edytor tekstu) chciałem dodać do niego funkcje kopiuj wklej. Tylko problem polega na tym że wklejeniu powiela mi tekst wcześniejszy + dodaje skopiowany+tylny, a chciałbym żeby dany tekst pojawił się w miejscu gdzie jest kursor
kod kopiuj
kopiuj.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
{
StringSelection data = new StringSelection(text.getSelectedText());
clipboard.setContents(data, data);
}}
});
kod wklej
wklej.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Transferable clipData = clipboard.getContents(this);
String s;
try {
s = (String) (clipData.getTransferData(DataFlavor.stringFlavor));
}
catch (Exception ex) {
s = ex.toString();
}
String po;
// String przed;
try {
String przed=text.getText(0, text.getCaretPosition());
po = text.getText(0, text.getText().length());
text.setText("");
text.setText(przed + s+po);
} catch (BadLocationException ex) {
//System.out.print("tu jest błąd\n"+k);
}
// text.setText(text.getText()+s);
// text.setText(text.getText()+s);
}
});