CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 1999
    Posts
    492

    JLayeredPane : How to get component at a specific point and ignore a layer

    I am working on an application that uses a JLayeredPane. I'm using the drag layer for a "drag-n-move" operation and I would like to determine what other child component the drag is occurring upon.

    getComponentAt() is what I was using, however it will always return the item in the drag layer that's currently being moved because it's always going to be under the cursor!

    Considering the drag layer is intended to be used for dragging stuff around I find it rather surprising that the JLayeredPanel has no default or special version of getComponentAt() to handle situations like these.

    I think from the member functions of JLayeredPanel and Component I can build my own version of a function like this, but I'm surprised one doesn't already exist.
    Last edited by SteveS; March 27th, 2012 at 11:48 AM. Reason: JLayeredPane
    Why are the "tolerant" so easy to offend?

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JLayeredPane : How to get component at a specific point and ignore a layer

    I'm not sure what exactly you are trying to achieve. Do you mean you want to get all the components on a given layer that are at a specified location or all components on all layers at the specified location?

    Either way, surprisingly, there is no method to do this but you can easily write something. You just need to get all the components on the layer you want to check and iterate over the components, adding each component whose contains(..) method returns true to a list and finally return the list.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Aug 1999
    Posts
    492

    Re: JLayeredPane : How to get component at a specific point and ignore a layer

    Basically the JLayeredPane's getComponentAt() will literally return the component at the spot you specify, so if you are using the drag layer to move a component around a call to getComponentAt() will always return the item you are dragging because it will always be the component directly under the cursor. In those situations you are really interested in what's underneath that cursor.


    In my situation there's only 2 components to check, and they are containers that will then handle that point internally, so my own code would be easy to make.


    This just seems like a situation that others had to have encountered before, but I have seen nothing in my searches. I'm not even thrilled with the JLayeredPane's features for making a general-function for all situations because you can't really iterate through all of the used layers in the panel.
    Why are the "tolerant" so easy to offend?

  4. #4
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: JLayeredPane : How to get component at a specific point and ignore a layer

    Maybe this link will help, if you haven't read it already:
    Lesson: Drag and Drop and Data Transfer

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JLayeredPane : How to get component at a specific point and ignore a layer

    I'm not even thrilled with the JLayeredPane's features for making a general-function for all situations because you can't really iterate through all of the used layers in the panel.
    Yes there does appear to be a lack of functionality, you would have thought it would have methods like getComponentsAt(), getLayers() etc.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Aug 1999
    Posts
    492

    Re: JLayeredPane : How to get component at a specific point and ignore a layer

    Quote Originally Posted by Martin O View Post
    Maybe this link will help, if you haven't read it already:
    Lesson: Drag and Drop and Data Transfer
    Thanks, but I initially headed in that direction, but it had a few problems:

    1) The item being dragged remains on-screen while I need to make it look like the object is being moved.

    2) The mouse cursor changes as you drag over other objects.

    3) Java turns the drag image semi-transparent.

    I think I could have gotten around problems 1 & 2, but problem #3 was unsolvable for me. At that point I decided it would be easier to manually respond to MouseAdapter events than to modify true drag and drop to fit my needs.

    I think my main problem with drag and drop came from the fact that what I'm doing doesn't really fit the concept of drag and drop because instead of taking data or attributes from one object and moving/copying to another I am moving the object itself. I know that even objects are data, but I think the subtle difference in concept is why true drag and drop didn't fit my needs.
    Why are the "tolerant" so easy to offend?

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JLayeredPane : How to get component at a specific point and ignore a layer

    I think my main problem with drag and drop came from the fact that what I'm doing doesn't really fit the concept of drag and drop
    The drag and drop mechanism is a transfer mechanism to transfer objects/data from one component/container/application to another, whereas it sounds like what you are doing is repositioning a component within the same container.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured