JTextField: paste()
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class Main {
JTextField jtf = new JTextField(15);
JButton jbtnCut = new JButton("Cut");
JButton jbtnPaste = new JButton("Paste");
JButton jbtnCopy = new JButton("Copy");
public Main() {
JFrame jfrm = new JFrame("Cut, Copy, and Paste");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(230, 150);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jbtnCut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jtf.cut();
update();
}
});
jbtnPaste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jtf.paste();
update();
}
});
jbtnCopy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jtf.copy();
update();
}
});
jtf.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent ce) {
update();
}
});
jfrm.add(jtf);
jfrm.add(jbtnCut);
jfrm.add(jbtnPaste);
jfrm.add(jbtnCopy);
jfrm.setVisible(true);
}
private void update() {
System.out.println("All text: " + jtf.getText());
if (jtf.getSelectedText() != null)
System.out.println("Selected text: " + jtf.getSelectedText());
else
System.out.println("Selected text: ");
}
public static void main(String args[]) {
new Main();
}
}
Related examples in the same category