Results 1 to 14 of 14
- 04-02-2012, 06:28 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Best way to paste (from clipboard) to other windows in Windows
Hi!
I want to simple copy and paste text between my java program an another window. Example: Copy some text from java and paste it in Notepad. All stuff about getting foreground-background windows and copy to the clipboard it's done (I'm using JNA and Clipboard class), only what I need is to do paste (ctrl+v). Please tell me the best way to do that. Thanks.
- 04-02-2012, 07:13 AM #2
Re: Best way to paste (from clipboard) to other windows in Windows
Why are you using JNA and Clipboard? JTextComponent has that functionality built in, along with appropriate Key Bindings. If you need programmatic access, there are public methods for cut/copy/paste.
As far as pasting the text in another application is concerned, I don't know of any foolproof approach. One chancy way of doing it is by using java.awt.Robot.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-02-2012, 07:29 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Re: Best way to paste (from clipboard) to other windows in Windows
Thanks for the reply DarrylBurke. I used JNA and Clipboard because what I want is to copy and paste to another program not written in java, like Notepad. Robot is for only java programs, or I'm wrong?
- 04-02-2012, 07:40 AM #4
Re: Best way to paste (from clipboard) to other windows in Windows
You're wrong on both accounts.
Have you gone through the API for JTextComponent and Robot?
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-02-2012, 08:08 AM #5
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Re: Best way to paste (from clipboard) to other windows in Windows
Nop, I dismissed Robot because I read somewhere that it is not recommended for use in production environments and I viewed some Robot examples for testing only java programs. So is not true, isn't it?. Switching between different programs in windows can be done without using JNA or JNI?
- 04-02-2012, 08:16 AM #6
Re: Best way to paste (from clipboard) to other windows in Windows
Switching between windows is an entirely different kettle of fish, and something I've never tried. I guess that might be doable, in a system dependent manner, with JNI/JNA.
You copy to the clipboard from your Java text component using its copy() method. Once you have switched windows you can instruct Robot to send a Ctrl+V to paste the content.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-02-2012, 08:38 AM #7
Re: Best way to paste (from clipboard) to other windows in Windows
Another approach might be to write out and execute a VBA Script. Note that the sleep time (500 ms) is arbitrary and is to allow Notepad to load fully. Also that the user can mess things up by bringing another window to the front before the paste takes place.
Java Code:import java.io.File; import java.io.FileWriter; import javax.swing.JTextField; public class PasteToNotepad { public static void main(String[] args) throws Exception { String text = "Some text for testing."; JTextField textField = new JTextField(text); textField.setSelectionStart(0); textField.setSelectionEnd(text.length() - 1); textField.copy(); String vbs = "" + "Set WshShell = WScript.CreateObject(\"WScript.Shell\")\n" + "WshShell.Run \"notepad\", 9\n" + "WScript.Sleep 500\n" + "WshShell.SendKeys \"^V\""; File file = File.createTempFile("PrintDialog", ".vbs"); file.deleteOnExit(); FileWriter fw = new java.io.FileWriter(file); fw.write(vbs); fw.close(); Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath()); p.waitFor(); } }
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-02-2012, 08:38 AM #8
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Re: Best way to paste (from clipboard) to other windows in Windows
Excellent, I just did a test using Robot and worked fine. In this case I can't use JTextComponent because the text comes from the code. So I still have to use JNA for managing windows, but no matter. Thanks db!!
- 04-02-2012, 08:42 AM #9
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Re: Best way to paste (from clipboard) to other windows in Windows
Thanks for the example!, it's a good idea too.
- 04-02-2012, 08:47 AM #10
- 04-02-2012, 08:58 AM #11
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Re: Best way to paste (from clipboard) to other windows in Windows
Yeap, this example can help me for other part of my program for opening new windows, but in this particulary case I must switch between windows already opened. Thanks again db.
- 07-19-2012, 08:49 PM #12
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
Re: Best way to paste (from clipboard) to other windows in Windows
Last edited by DarrylBurke; 07-20-2012 at 08:53 PM. Reason: Removed annoying oversize red bold formatting
- 07-19-2012, 09:11 PM #13
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 11
Re: Best way to paste (from clipboard) to other windows in Windows
^Please, open new thread and ask that question, don't hijack old threads.
- 07-20-2012, 08:58 PM #14
Re: Best way to paste (from clipboard) to other windows in Windows
1. Please don't post to old dead threads. When you have a question, start your own thread -- they're free. Feel free to link to any other related thread.
2. Please keep technical questions on the forum. Private messaging will NOT get you a better or faster response. Nor will requesting an early response. Any urgency on your part is not our concern.
3. Please don't misuse the formatting features in a misguided attempt to draw attention to your post. That will have the opposite effect, as entire paragraphs in large bold red font are more than likely to be perceived as annoying.
I'm closing this thread. Feel free to ask your question, with adequate details, in a new thread. It might help you to get a better response if you first go through this web page: How to ask questions the smart way
db
THREAD CLOSEDIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
For my Applet Application how can i use system clipboard options. like copy paste,cut
By cnu.nandhikonda@gmail.com in forum Java AppletsReplies: 5Last Post: 02-08-2012, 03:17 PM -
Windows java vs Windows AIX
By sysp in forum New To JavaReplies: 0Last Post: 01-26-2011, 06:32 AM -
How to copy and paste data with the clipboard
By Java Tip in forum SWT TipsReplies: 0Last Post: 07-07-2008, 05:35 PM -
Requesting help in copy paste of folder similar to windows.
By selvin_raj in forum Advanced JavaReplies: 1Last Post: 06-23-2008, 07:46 AM -
swt for windows
By Gajesh Tripathi in forum SWT / JFaceReplies: 2Last Post: 10-18-2007, 07:43 AM
Bookmarks