• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

d'n'd any component

 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
im playing around with dnd in swing apps. here is what i want to do:
in a JPanel i have any kind of Components (say a JButton, JLabel...). the user should be able to drag those around and put them in a new position (null-layout).
how do i find out what Component to move ? all the examples i saw were using JTree or JList and they make it easy to find the currently selected Node or Entry.
thanks for any help
karl
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karl, I had been trying to achieve a similar goal. And I could not find a suitable true Drag and Drop way of doing it. So here is what I did.
I regestered a mouse listener on my JPanel where all my components were located. As the mouse moved around the JPanel at each X and Y coord, I checked for a component in that location. Then when I clicked the mouse button to begin dragging, if a component existed I would then do this process.
Remove Component
Repaint
Add Component - with the dragged x and y of the mouse now as
long as the button is still pressed
Repaint
This process worked except I had a problem with some components. For instance a JTextField. Once you enter the Text area with your mouse, the listener for the JPanel was ignored because technically you are inside the JTextArea and not on the JPanel any more. I played around with several ideas. For instance, if you were able make the app to where you had to push a button when you wanted to drag something, you could diable all components in the JPanel when this button or toggle switch is pressed and then you would be able to grab components I think.
Hope that at least gives you a theory to try out.

------------------
Happy Coding,
Gregg Bolinger
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i thought of this too. since i started doing this kind of thing to learn about java d'nd features i didnt try that way.
i think to do the thing we wanna do, one must subclass all Components which should be draggable. so the all act as listeners for dnd. i dont like this too much. here you can find an dnd faq and a dnd framework for free download . i never tested the framework so i dont know if its any good. but you get the source also and so you can check how other people do.
karl
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic