TransferHandlers and JPanels

Discussion in 'Java' started by Jason Cavett, Mar 13, 2007.

  1. Jason Cavett

    Jason Cavett Guest

    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.
     
    Jason Cavett, Mar 13, 2007
    #1
    1. Advertising

  2. 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/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)?

    --
    Thomas
     
    Thomas Fritsch, Mar 14, 2007
    #2
    1. Advertising

  3. Jason Cavett

    Jason Cavett Guest

    On Mar 13, 11:34 pm, Thomas Fritsch <>
    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/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)?
    >
    > --
    > 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.
     
    Jason Cavett, Mar 14, 2007
    #3
  4. 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
     
    Thomas Fritsch, Mar 14, 2007
    #4
  5. Jason Cavett

    Jason Cavett Guest

    On Mar 14, 6:35 am, Thomas Fritsch <>
    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.
     
    Jason Cavett, Mar 14, 2007
    #5
  6. Jason Cavett

    Jason Cavett Guest

    On Mar 14, 3:35 pm, "Jason Cavett" <> wrote:
    > On Mar 14, 6:35 am, Thomas Fritsch <>
    > 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?
     
    Jason Cavett, Mar 14, 2007
    #6
  7. 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
     
    Thomas Fritsch, Mar 15, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oxnard
    Replies:
    1
    Views:
    6,151
    Andrew Thompson
    Nov 18, 2004
  2. titi

    if and and vs if and,and

    titi, Mar 9, 2007, in forum: VHDL
    Replies:
    4
    Views:
    593
    Mike Treseler
    Mar 11, 2007
  3. pek
    Replies:
    6
    Views:
    780
    Andrew Thompson
    Aug 2, 2007
  4. Jason Cavett

    Delete Using TransferHandlers

    Jason Cavett, Jun 12, 2008, in forum: Java
    Replies:
    0
    Views:
    268
    Jason Cavett
    Jun 12, 2008
  5. Hooch
    Replies:
    0
    Views:
    326
    Hooch
    Nov 2, 2009
Loading...

Share This Page