Copy and pasting

Copy and pasting

hi all, I am copying some cells from excell... JPopupMenu popup; JTable table; public BasicAction cut, copy, paste, selectAll...); copy = new CopyAction("Copy", null); paste = new PasteAction("Paste",null)

View Answers

April 15, 2008 at 8:02 PM

import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.table.*;

public class TCPopupEventQueue extends EventQueue {
public JPopupMenu popup;
JTable table;
public BasicAction cut, copy, paste, selectAll;

public TCPopupEventQueue() {
//createPopupMenu();
}

public void createPopupMenu(JTextComponent text){
cut = new CutAction("Cut",null);
copy = new CopyAction("Copy", null);
paste = new PasteAction("Paste",null);
selectAll = new SelectAllAction("Select All",null);
cut.setTextComponent(text);
copy.setTextComponent(text);
paste.setTextComponent(text);
selectAll.setTextComponent(text);

popup = new JPopupMenu();
popup.add( cut );
popup.add( copy );
popup.add( paste );
popup.addSeparator();
popup.add( selectAll );
}

public void showPopup(Component parent, MouseEvent me){
popup.validate();
popup.show(parent, me.getX(), me.getY());
}

protected void dispatchEvent(AWTEvent event){
super.dispatchEvent(event);
if(!(event instanceof MouseEvent)){
return;
}
MouseEvent me = (MouseEvent)event;
if(!me.isPopupTrigger()) {
return;
}
if( !(me.getSource() instanceof Component) ) {
return;
}
Component comp = SwingUtilities.getDeepestComponentAt((Component)me.getSource(),me.getX(), me.getY());
if( !(comp instanceof JTextComponent)){
return;
}
if(MenuSelectionManager.defaultManager().getSelectedPath().length > 0){
return;
}
createPopupMenu((JTextComponent)comp);
showPopup((Component)me.getSource(), me);
}
public abstract class BasicAction extends AbstractAction{
JTextComponent comp;

public BasicAction(String text, Icon icon) {
super(text, icon);
putValue(Action.SHORT_DESCRIPTION, text);
}
public void setTextComponent(JTextComponent comp){
this.comp = comp;
}
public abstract void actionPerformed(ActionEvent e);
}
public class CutAction extends BasicAction {
public CutAction(String text, Icon icon) {
super(text, icon);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl X"));
}
public void actionPerformed(ActionEvent e){
comp.cut();
}
public boolean isEnabled(){
return comp != null && comp.isEditable() && comp.getSelectedText() != null;
}
}
public class CopyAction extends BasicAction{
public CopyAction(String text, Icon icon){
super(text,icon);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl C"));
}
public void actionPerformed(ActionEvent e) {
comp.copy();
}
public boolean isEnabled() {
return comp != null && comp.getSelectedText() != null;
}
}
public class PasteAction extends BasicAction{
public PasteAction(String text, Icon icon){
super(text,icon);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl V"));
}
public void actionPerformed(ActionEvent e) {
comp.paste();
}
public boolean isEnabled() {
Transferable content = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
return comp != null && comp.isEnabled() && comp.isEditable()
&& content.isDataFlavorSupported(DataFlavor.stringFlavor);
}
}

public class SelectAllAction extends BasicAction{
public SelectAllAction(String text, Icon icon){
super(text,icon);
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl A"));
}
public void actionPerformed(ActionEvent e){
comp.selectAll();
}
public boolean isEnabled() {
return comp != null && comp.isEnabled() && comp.getText().length() > 0
&& (comp.getSelectedText() == null ||
comp.getSelectedText().length() < comp.getText().length());
}
}
public static void main(String[] args) {
Toolkit.getDefaultToolkit().getSystemEventQueue().push( new TCPopupEventQueue());
JTextField field = new JTextField(20);
JTextField field1 = new JTextField(20);
JPanel center = new JPanel( new FlowLayout(FlowLayout.LEFT) );


center.add(new JLabel("cell1:"));
center.add(field);
center.add(new JLabel("cell2:"));
center.add(field1);
JPanel content = new JPanel( new FlowLayout(FlowLayout.LEFT) );
content.add( center, BorderLayout.SOUTH );
JFrame frame = new JFrame("cell copy past program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(content);
frame.setSize(550,100);
frame.setVisible(true);
}
}









Related Tutorials/Questions & Answers:
Copy and pasting - Java Beginners
Copy and pasting  hi all, I am copying some cells from excell... JPopupMenu popup; JTable table; public BasicAction cut, copy, paste, selectAll...); copy = new CopyAction("Copy", null); paste = new PasteAction("Paste",null)  
Array copy
Array copy  Hello all. could someone telle me how to copy an array (holding random numbers) to a new array...( then make randomnumbers on this new array ) but without changing the excisting array ? last . possibility
Advertisements
Posting Copy and Paste HTML
Posting Copy and Paste HTML   Posting Copy and Paste HTML
copy() example
Copying a file in PHP In this example, you will learn 'how to copy a file in PHP programming language?' For copying a file, we use copy command from source to destination within PHP code tag like ADS_TO_REPLACE_1 copy ($source
cut,copy,paste
cut,copy,paste  plz... give me the coding of cut,copy and paste in Java (Netbeans
Copy .mdb file for backup.
Copy .mdb file for backup.  hi.. i am new to java... in my application , at one place for taking back up i have to copy .mdb file from one place... tell me for multiple files of multiple type...how we can copy.. thank you
copy file - Java Beginners
copy file  i want to copy file from one folder to another   Hi Friend, Try the following code: import java.io.*; public class CopyFile{ public void copy(File src, File dst) throws IOException
data copy - SQL
data copy   how to copy values of one column from one table into another column of another table?  Hi INSERT INTO table_name (column_name) AS SELECT DISTINCT VALUES FROM table_name (column_name
Copy Files - Java Beginners
Copy Files  I saw the post on copying multiple files (http://www.roseindia.net/java/example/java/io/CopyMultipleFiles.shtml) and I have something... and then copy it to the folder I need. Does anyone have a similar program
JavaScript Copy Array
JavaScript Copy Array     ... useful in complex applications. In this section, you will study how to copy an array...; <h2>Copy Array Example</h2> <script language="javascript
Copy Mails to Xl sheet - JavaMail
Copy Mails to Xl sheet  Can somebody give me the Java code to copy outlook mails into an Xl sheet
ModuleNotFoundError: No module named 'copy-pase'
ModuleNotFoundError: No module named 'copy-pase'  Hi, My Python... 'copy-pase' How to remove the ModuleNotFoundError: No module named 'copy... have to install padas library. You can install copy-pase python with following
ModuleNotFoundError: No module named 'copy-pase'
ModuleNotFoundError: No module named 'copy-pase'  Hi, My Python... 'copy-pase' How to remove the ModuleNotFoundError: No module named 'copy... have to install padas library. You can install copy-pase python with following
ModuleNotFoundError: No module named 'krunk-copy'
ModuleNotFoundError: No module named 'krunk-copy'  Hi, My Python... 'krunk-copy' How to remove the ModuleNotFoundError: No module named 'krunk-copy' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'micropython-copy'
ModuleNotFoundError: No module named 'micropython-copy'  Hi, My... named 'micropython-copy' How to remove the ModuleNotFoundError: No module named 'micropython-copy' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'pycopy-copy'
ModuleNotFoundError: No module named 'pycopy-copy'  Hi, My Python... 'pycopy-copy' How to remove the ModuleNotFoundError: No module named 'pycopy-copy' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'sh-copy'
ModuleNotFoundError: No module named 'sh-copy'  Hi, My Python...-copy' How to remove the ModuleNotFoundError: No module named 'sh-copy... to install padas library. You can install sh-copy python with following command
ModuleNotFoundError: No module named 'sh-copy'
ModuleNotFoundError: No module named 'sh-copy'  Hi, My Python...-copy' How to remove the ModuleNotFoundError: No module named 'sh-copy... to install padas library. You can install sh-copy python with following command
ModuleNotFoundError: No module named 'temp-copy'
ModuleNotFoundError: No module named 'temp-copy'  Hi, My Python... 'temp-copy' How to remove the ModuleNotFoundError: No module named 'temp-copy' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'temp-copy'
ModuleNotFoundError: No module named 'temp-copy'  Hi, My Python... 'temp-copy' How to remove the ModuleNotFoundError: No module named 'temp-copy' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'temp-copy'
ModuleNotFoundError: No module named 'temp-copy'  Hi, My Python... 'temp-copy' How to remove the ModuleNotFoundError: No module named 'temp-copy' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'temp-copy'
ModuleNotFoundError: No module named 'temp-copy'  Hi, My Python... 'temp-copy' How to remove the ModuleNotFoundError: No module named 'temp-copy' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'wet_copy'
ModuleNotFoundError: No module named 'wet_copy'  Hi, My Python..._copy' How to remove the ModuleNotFoundError: No module named 'wet_copy... to install padas library. You can install wet_copy python with following
ModuleNotFoundError: No module named 'wet_copy'
ModuleNotFoundError: No module named 'wet_copy'  Hi, My Python..._copy' How to remove the ModuleNotFoundError: No module named 'wet_copy... to install padas library. You can install wet_copy python with following
ModuleNotFoundError: No module named 'copy-pase'
ModuleNotFoundError: No module named 'copy-pase'  Hi, My Python... 'copy-pase' How to remove the ModuleNotFoundError: No module named 'copy... have to install padas library. You can install copy-pase python with following
copy string to string objective c
copy string to string objective c  How to copy a value of NSString to another string in Objective C? I have to different views and i am will to show the NSString value in both of them. Thanks
Java Copy file example
Copy one file into another       Copy one file into another In this section, you will learn how to copy content of one file into another file. We
Creating a Copy of a Collection
Creating a Copy of a Collection   ... and then copy the collection. The copied collection contains the reference... a collection (list) and copy for the duplicate collection with content
Ad Copy,Ad Copy Services,Effective Copy for Ads,Ad Copy Outsourcing India
Ad Copy Writing effective copy for ads, web pages, sales letters and other... successful advertising copy. These rules apply to writing copy for any type... copy produces the biggest response when each reader can believe the message
Copy Table in a MySQL Database
Copy Table in a MySQL Database       In this section, you will learn to copy one table to another... you in copying one table to another in a same MySQL database. To copy any table
How to copy a file
How to copy a file In this section, you will learn how to copy a file using Java IO library. For this, we have declared a function called copyfile() which...;} } The above code makes a copy of the file
Copy one file into another
Copy one file into another In this section, you will learn how to copy content of one file into another file. We will perform this operation by using the read & write methods of BufferedWriter class. Given below example will give
Copy Directory or File in Java
Copy Directory in Java      ... how to copy the directory and files into a new directory. In this program, you... srcPath, File dstPath): The copyDirectory() method is used to copy contents
How to copy a file in java
How to copy a file in java In this section you will learn about how to copy... direct way to copy a  file.  What we can do is, read a content of one...; FileUtils.copyFile(source file , destinationfile), which allows you to copy a file
Copy file in php, How to copy file in php, PHP copy file
Learn How to copy file in php. The PHP copy file example explained here...; In the following example we will show you how to use of copy() function in php. The copy function in PHP is used to copy the content of one file into another
copy something from a webpage to my webpage
copy something from a webpage to my webpage  How can I copy something from a webpage to my webpage
ModuleNotFoundError: No module named 'aws-ssm-copy'
ModuleNotFoundError: No module named 'aws-ssm-copy'  Hi, My Python... 'aws-ssm-copy' How to remove the ModuleNotFoundError: No module named 'aws-ssm-copy' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'carthage-copy-frameworks'
ModuleNotFoundError: No module named 'carthage-copy-frameworks'  Hi...: No module named 'carthage-copy-frameworks' How to remove the ModuleNotFoundError: No module named 'carthage-copy-frameworks' error? Thanks  
ModuleNotFoundError: No module named 'django-pg-copy'
ModuleNotFoundError: No module named 'django-pg-copy'  Hi, My... named 'django-pg-copy' How to remove the ModuleNotFoundError: No module named 'django-pg-copy' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'mac-copy-capture'
ModuleNotFoundError: No module named 'mac-copy-capture'  Hi, My... named 'mac-copy-capture' How to remove the ModuleNotFoundError: No module named 'mac-copy-capture' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'mssh-copy-id'
ModuleNotFoundError: No module named 'mssh-copy-id'  Hi, My Python... 'mssh-copy-id' How to remove the ModuleNotFoundError: No module named 'mssh-copy-id' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'odoo8-addon-web-copy-confirm'
ModuleNotFoundError: No module named 'odoo8-addon-web-copy-confirm'  ...: No module named 'odoo8-addon-web-copy-confirm' How to remove the ModuleNotFoundError: No module named 'odoo8-addon-web-copy-confirm' error
ModuleNotFoundError: No module named 'pg-stream-copy'
ModuleNotFoundError: No module named 'pg-stream-copy'  Hi, My... named 'pg-stream-copy' How to remove the ModuleNotFoundError: No module named 'pg-stream-copy' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'ppa-copy-packages'
ModuleNotFoundError: No module named 'ppa-copy-packages'  Hi, My... named 'ppa-copy-packages' How to remove the ModuleNotFoundError: No module named 'ppa-copy-packages' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'pybuilder-smart-copy-resources'
ModuleNotFoundError: No module named 'pybuilder-smart-copy-resources' ...: No module named 'pybuilder-smart-copy-resources' How to remove the ModuleNotFoundError: No module named 'pybuilder-smart-copy-resources' error
ModuleNotFoundError: No module named 'pybuilder-smart-copy-resources'
ModuleNotFoundError: No module named 'pybuilder-smart-copy-resources' ...: No module named 'pybuilder-smart-copy-resources' How to remove the ModuleNotFoundError: No module named 'pybuilder-smart-copy-resources' error
ModuleNotFoundError: No module named 'remote_copy_and_execute'
ModuleNotFoundError: No module named 'remote_copy_and_execute'  Hi...: No module named 'remote_copy_and_execute' How to remove the ModuleNotFoundError: No module named 'remote_copy_and_execute' error? Thanks   
ModuleNotFoundError: No module named 'sqlalchemy-postgres-copy'
ModuleNotFoundError: No module named 'sqlalchemy-postgres-copy'  Hi...: No module named 'sqlalchemy-postgres-copy' How to remove the ModuleNotFoundError: No module named 'sqlalchemy-postgres-copy' error? Thanks  
ModuleNotFoundError: No module named 'aws-ssm-copy'
ModuleNotFoundError: No module named 'aws-ssm-copy'  Hi, My Python... 'aws-ssm-copy' How to remove the ModuleNotFoundError: No module named 'aws-ssm-copy' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'booru-note-copy'
ModuleNotFoundError: No module named 'booru-note-copy'  Hi, My... named 'booru-note-copy' How to remove the ModuleNotFoundError: No module named 'booru-note-copy' error? Thanks   Hi, In your python

Ads