Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > TransferHandlers and JPanels

Reply
Thread Tools

TransferHandlers and JPanels

 
 
Jason Cavett
Guest
Posts: n/a
 
      03-13-2007
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/tutor...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.

 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      03-14-2007
Jason Cavett wrote:
> 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/tutor...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)?

--
Thomas
 
Reply With Quote
 
 
 
 
Jason Cavett
Guest
Posts: n/a
 
      03-14-2007
On Mar 13, 11:34 pm, Thomas Fritsch <(E-Mail Removed)>
wrote:
> Jason Cavett wrote:
> > 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/tutor...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)?
>
> --
> 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.

 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      03-14-2007
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.

--
Thomas
 
Reply With Quote
 
Jason Cavett
Guest
Posts: n/a
 
      03-14-2007
On Mar 14, 6:35 am, Thomas Fritsch <(E-Mail Removed)>
wrote:
> 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.
>
> --
> 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.

 
Reply With Quote
 
Jason Cavett
Guest
Posts: n/a
 
      03-14-2007
On Mar 14, 3:35 pm, "Jason Cavett" <(E-Mail Removed)> wrote:
> On Mar 14, 6:35 am, Thomas Fritsch <(E-Mail Removed)>
> wrote:
>
> > 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.

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


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?

 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      03-15-2007
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?



--
Thomas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generate jPanels based on parameter Hooch Java 0 11-02-2009 08:22 PM
Delete Using TransferHandlers Jason Cavett Java 0 06-12-2008 08:14 PM
Custom JPanels inside another JPanel. pek Java 6 08-02-2007 01:51 PM
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM
Events with multiple JPanels and JButtons Oxnard Java 1 11-18-2004 03:06 AM



Advertisments