TransferHandlers and JPanels

J

Jason Cavett

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.
 
T

Thomas Fritsch

Jason said:
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.
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)?
 
J

Jason Cavett

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)?
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.
 
T

Thomas Fritsch

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.
 
J

Jason Cavett

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.

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.
 
J

Jason Cavett

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.

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.

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?
 
T

Thomas Fritsch

Jason said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top