Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

TransferHandlers and JPanels

Thread view: 
Jason Cavett - 13 Mar 2007 20:57 GMT
I have successfully implemented cut/copy/paste in a JTree by
implmenting my own TransferHandler and Transferrable object.  It works
great and I am very impressed with the underlying support and how
relatively easy it was to understand and implement.

I am attempting to do the same thing with a JPanel.  This JPanel holds
JLabels and I want to be able to cut/copy/paste the labels within that
panel.  According to the drag and drop tutorial (found at:
http://java.sun.com/docs/books/tutorial/uiswing/dnd/intro.html) a
JPanel does not provide out-of-the-box support for cut/copy/paste, but
I should be able to write my own implementation to handle this.

I tried to do something similar to what I did with the JTree, but, for
some reason, my JPanel is not triggering the cut/copy/paste event.
I'm guessing this has something to do with the non-out-of-the-box
support, but I'm not really sure.

Does anybody have any suggestions or ideas of where to look next?
Thanks.
Thomas Fritsch - 14 Mar 2007 04:34 GMT
> I have successfully implemented cut/copy/paste in a JTree by
> implmenting my own TransferHandler and Transferrable object.  It works
[quoted text clipped - 12 lines]
> I'm guessing this has something to do with the non-out-of-the-box
> support, but I'm not really sure.
Which JComponent do you want to support cut/copy/paste, the JLabels or
the JPanel?

Did you set up a TransferHandler for your JComponent?

Did you set up the input/action-mappings for your JComponent, like it is
described in the paragraph "Adding Cut/Copy/Paste Support" of the
tutorial you mentioned above? Something like:
  JComponent c = ...;  // your JComponent
  InputMap inputMap = c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
  inputMap.put(KeyStroke.getKeyStroke("control X"), "cut");
  inputMap.put(KeyStroke.getKeyStroke("control C"), "copy");
  inputMap.put(KeyStroke.getKeyStroke("control V"), "paste");
  ActionMap actionMap = c.getActionMap();
  actionMap.put("cut",   TransferHandler.getCutAction());
  actionMap.put("copy",  TransferHandler.getCopyAction());
  actionMap.put("paste", TransferHandler.getPasteAction());
(Otherwise the TransferHandler of your JComponent won't be called.)

Did you implement your own subclass of TransferHandler (and override the
important methods) or did you use the TransferHandler("bla") constructor
(thus relying on the getBla/setBla methods of your JComponent)?

Signature

Thomas

Jason Cavett - 14 Mar 2007 06:23 GMT
On Mar 13, 11:34 pm, Thomas Fritsch <i.dont.like.s...@invalid.com>
wrote:
> > I have successfully implemented cut/copy/paste in a JTree by
> > implmenting my own TransferHandler and Transferrable object.  It works
[quoted text clipped - 38 lines]
> --
> Thomas

> Which JComponent do you want to support cut/copy/paste, the JLabels or
the JPanel?

The JLabels will be the actual things that are cut/copy/pasted but
they will be cut/copy/pasted into the JPanel.  So, I want to support
cut/copy/paste in the JPanel.  My underlying TransferHandler handles
the labels.

> Did you set up a TransferHandler for your JComponent?

Yes

> Did you set up the input/action-mappings for your JComponent

Ah...I didn't do that.  I didn't think I had to.  So far, everything
has "just worked."  (I have already done this for a JTree.)  Do you
think I need to do this for the JPanel when it already works in the
JTree?

> Did you implement your own subclass of TransferHandler

Yes.  Similar to what I did with my own JTree implementation, which
works great.

I'll look into the action mappings as that would make sense for the
problem I'm having.  If you have any other suggestions based on my
feedback, I'd be interested in hearing that, too.

Thanks for your help.
Thomas Fritsch - 14 Mar 2007 11:35 GMT
Jason Cavett schrieb:
[...]
> If you have any other suggestions based on my
> feedback, I'd be interested in hearing that, too.
My IDE with its debugger has helped me a lot to understand what is
really going on.
Therefore I suggest: Launch your favorite IDE, set a breakpoint in
method TransferHandler$TransferAction.actionPerformed (which is the
common core of TransferAction.getCut/Copy/PasteAction), start your
application, type control-C (-> the debugger should break now), walk
through step-by-step, and see how things work.

Signature

Thomas

Jason Cavett - 14 Mar 2007 20:35 GMT
On Mar 14, 6:35 am, Thomas Fritsch <i.dont.like.s...@invalid.com>
wrote:
> Jason Cavett schrieb:
> [...]> If you have any other suggestions based on my
[quoted text clipped - 10 lines]
> --
> Thomas

Sweet!  It works.  Thank you very much.

I am still having a problem in that my toolbar cannot trigger actions
on a component (cut/copy/paste) even though the menu bar works fine.
I followed the instructions in the tutorial again, but when I click a
button in the (graphical) toolbar, the action does not seem to
remember what component last had focus like it does when I click on an
item in the menu bar.

Would you happen to know anything about this?

Again, thank you very much for your help.  It is/was much appreciated.
Jason Cavett - 14 Mar 2007 21:35 GMT
> On Mar 14, 6:35 am, Thomas Fritsch <i.dont.like.s...@invalid.com>
> wrote:
[quoted text clipped - 26 lines]
>
> Again, thank you very much for your help.  It is/was much appreciated.

Well, I have figured out one way to prevent the buttons from gaining
focus when I click on them and that's by setting them to
isFocusable(false).  It works, but it seems *somewhat* hackish.

What do you think?
Thomas Fritsch - 15 Mar 2007 14:18 GMT
Jason Cavett schrieb:
> Well, I have figured out one way to prevent the buttons
...you're talking about the toolbar-buttons, don't you?
> from gaining
> focus when I click on them and that's by setting them to
> isFocusable(false).  It works, but it seems *somewhat* hackish.
Not necessarily.
May be there is a convention whether toolbar-buttons should be
non-focusable. I've looked through some native Windows-apps (Mozilla,
IE), and they all have non-focusable toolbar-buttons (i.e. they are not
reachable by the TAB-key).

> What do you think?

Signature

Thomas



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.