Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Copy and pasting
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


vijay
Copy and pasting
1 Answer(s)      6 years and 8 months ago
Posted in : Java Beginners


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)


Advertisement
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
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() 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  copy ($source,$destination); Lets
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
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
JavaScript Copy Array
JavaScript Copy Array     ... useful in complex applications. In this section, you will study how to copy an array... using JavaScript.. <html> <head> <h2>Copy
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
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 file
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 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
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 Table in a MySQL Database
Copy Table in a MySQL Database       In this section, you will learn to copy one table to another... one table to another in a same MySQL database. To copy any table,  firstly
File copy through intranet - Java Beginners
File copy through intranet  Can i copy files from system A to System B connected through intranet?? Is this possible through java IO? If yes, please let me know
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... dstPath): The copyDirectory() method is used to copy contents from
PDF Copy(File) has many pages.
PDF Copy(File) has many pages.  Dear All, I make Jasper Report it compile ,run and view properly. when i am try to send it pdf format it also sent it it pdf properly. but a that PDF Copy(File) has many pages. single copy
copy file from folder to folder - Java Beginners
copy file from folder to folder   my requirement is I need to copy... wich contains the file to be copied. I want to copy the file from... is \\\\sap\\xi\\source\\Archive target folder is \\\\sap\\xi\\target I should copy
how to copy the directory in to ftp server using java
how to copy the directory in to ftp server using java  how to copy the directory in to ftp server using java ?   Hello venaktehs As much... working directory with the same name (which you wish to copy) as: issueCommand
How to copy text from a gif image
How to copy text from a gif image  I have some gif images that containing some important text. i want to have that text in my notepad file. i have.... so, please tell me how can i copy that text to my notepad. i am using ubuntu
File copy from one directory to another in java
File copy from one directory to another in java  I am trying to find... in Java which will help in writing code for copy the file in just few lines of code... directory? My program should be able to copy the file of any size. Our program does
Copy a Html chart into PPT - Java Beginners
Copy a Html chart into PPT  Hi Experts, i am facing with a problem without solution. I have a Html site and would like to write a code so that by clicking on a button the graph will be copied in an active ppt. Could you
FTP file copy problem? - Java Beginners
FTP file copy problem?  Hi, Can any one tell me how to set file lastModifiedTime after copying files through FTP. I am using commons-net-1.4.1.jar to copy files from local system to server. Eg: local file name ?test.doc
Copy char array into string
into it and the String.valueOf(charArray) copy the content of character
Advertisements
 

 

 

DMCA.com