We help IT Professionals succeed at work.

paste/copy under unix

ed_ed
ed_ed asked
on
Hi,

I made an app with a JMenu and JTextPane and I'd like to make a copy / paste function
So I wrote the following code :

if (JTextPane1.getSelectedText() != null)
{
    StringSelection lStringSelection = new StringSelection(JTextPane1.getSelectedText());
    Clipboard lClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    lClipboard.setContents(lStringSelection, lStringSelection);
}

I works very well on winnt but nothing happens on unix : I am currently using an emulator software to access to the unix machine

My question is :
How can I do copy/paste on unix with java ?
Comment
Watch Question

Commented:
how are you pasting with unix?  I mean in NT I assume you go to a different application, when the java app's contents are in your clipboard, and you type CTRL-v to paste.  Are you trying to do the same thing in unix?
CERTIFIED EXPERT

Commented:
If you're using Hummingbird exceed as your X server, you have to explicitly copy the win32 clipboard to the X selection and/or clipboard.

Unfortunately, the clipboard and primary X selection under X11 are two different things, and Swing only deals with the clipboard.

Working directly under X11, using an awt component will do the right thing (i.e. you can select text in an xterm and paste it into java or vice versa).

Author

Commented:
to smueller : on unix I am using the middle button of the mouse to paste text, so I'll try ctrl v but I believe that I have already tryed that

to stimpyjcat : I am using pc xware to emulate unix, and my app is ran from unix, so I'd like to copy text from a JTextPane to xemacs for example (as we can do this from xterms with mouse) So could you explain a little bit more your answer ?

Thanks

Commented:
Actually I meant not to use ctrl-v.  I was thinking maybe you were trying to use NT's mechanism for pasting, but unix is different.  Do you have the middle mouse button configured properly?  Well I guess you do if you're able to use it to paste between xterms...

Which version of java are you using?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Some cut&paste actions already exist.
Not sure if they'll help but maybe wortha try.
Have a look at:
DefaultEditorKit.PasteAction
DefaultEditorKit.CopyAction

CERTIFIED EXPERT

Commented:
CERTIFIED EXPERT

Commented:
CERTIFIED EXPERT

Commented:
CERTIFIED EXPERT

Commented:
CERTIFIED EXPERT
Commented:
grrrrr...

You won't be able to use the standard paste into XEmacs, because that relies on what is known as the "Primary Selection" in X.  While the AWT components copy into both the X Clipboard *and* the X Primary Selection buffer, the Swing components only use the clipboard.

You can get it to work exporting text from your Swing app, but you won't be able to import text w/o modifying XEmacs and your other apps to use the X clipboard, or you can wait for Sun to fix the bug in their Swing implementation.

There are some discussion forums on java.sun.com that cover this; to export text, you create a hidden awt text component and invoke copy on it whenever you do a copy on your swing text component; that enables the text export to work properly.

Unfortunately there's no reliable workaround to capture the appropriate events to paste into your Swing app from X.

Author

Commented:
to smueller : I am using java 1.3 and middle mouse button works very well on X apps !

to objects : I didn't looked for DefaultEditorKit.PasteAction and DefaultEditorKit.CopyAction
but I'll do it as soon as possible.

to stimpyjcat :
I think you are right : I found the following message on javasoft, if I understand correctly the message, I couldn't be able to use middle mouse button until I use java 1.4
--------------------------------------------------------
Bug Id  4197227
 
Votes  48
 
Synopsis  Allow cut/copy from Swing text comp to be pasted into std X win (via MMB, etc.)
 
Category  java:classes_swing
 
Reported Against  1.2beta3, 1.1.6, 1.0.1, 1.3
 
Release Fixed  merlin-beta
 
State  Closed, fixed
 
Related Bugs  4251234, 4404554
 
Submit Date  Dec 14, 1998
 
Description  


Original user synopsis:  "mouse cut-and-paste missing in Swing text components"

In normal AWT text components, you can select
text with the left mouse button and paste into
another X window with the middle button.
You can't do this with
Swing components, as far as I can tell.  This is
essential for X-window use.

Note that this is not the same as using copy(),
which copies to the X clipboard.  So there's not
even a workaround that I can figure out.

But perhaps I'm missing something?
(Review ID: 35184)



 
Workaround  


12/14/98 xxxxx@xxxxx   Text cut/copy'd from Swing text comp can be pasted
into any xterm/dtterm via the "PASTE" key, but cannot be pasted
into the X win using the standard middle-mouse-button paste convention.   If
you try to paste into a "raw" xterm, you get several blank lines.  If you
paste into a dtterm, you get a single blank line/char (no linefeed/CR).


 
Evaluation  Support for this feature has been added to the AWT in merlin. See
Toolkit.getSystemSelection(). javax.swing.text.JTextComponent must be updated
to use this new method.
xxxxx@xxxxx 2000-11-28

JTextComponent has functionality to fire caret events when the selection
changes.  This functionality has been updated to also use the TransferHandler
installed to transfer the currenct selection to the system selection clipboard.
DefaultCaret has been updated to use a single click the middle mouse button to
use the TransferHandler to import the contents of the system selection
clipboard into the component.  Alternative Caret implementations can of
course bind the paste differently.

I tested this on Linux, and transfers to the system selection buffer worked
fine.  A paste from the system selection buffer did not work because the
AWT level functionality gives a StringReader that immediately returns EOF.

xxxxx@xxxxx 2001-02-09

 

Explore More ContentExplore courses, solutions, and other research materials related to this topic.