import java.awt.Component;
import java.awt.Container;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Main extends JFrame {
JTextField tf;
JTextArea ta;
Clipboard clipboard = null;
Container objContainer = getContentPane();
public Main()
{
setTitle("Menu Example");
setSize(500, 500);
objContainer.add(tf = new JTextField(), "North");
objContainer.add(ta = new JTextArea(), "Center");
ta.setEditable(false);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("Edit");
menuBar.add(fileMenu);
JMenuItem copyAction = new JMenuItem("Copy...");
JMenuItem pasteAction = new JMenuItem("Paste...");
JMenuItem clearAction = new JMenuItem("Clear ...");
fileMenu.add(copyAction);
fileMenu.add(pasteAction);
fileMenu.add(clearAction);
copyAction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (clipboard == null)
clipboard = getToolkit().getSystemClipboard();
System.out.println("Inside Copy...");
StringSelection data;
data = new StringSelection(tf.getSelectedText());
clipboard.setContents(data, data);
System.out.println("Name:: "+clipboard.getName());
}
});
pasteAction.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("Inside Paste...");
Transferable clipData = clipboard.getContents(this);
String s;
try
{
s = (String) (clipData.getTransferData(DataFlavor.stringFlavor));
}
catch (Exception ee)
{
s = ee.toString();
}
ta.setText(s);
}
});
clearAction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("Inside Clear....");
tf.setText(null);
ta.setText(null);
}
});
}
public static void main(String[] args) {
Main me = new Main();
me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
me.setVisible(true);
}
}
No comments:
Post a Comment