Forums Register Login

d'n'd any component

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 969 times.
Similar Threads
Inheritance
Filtering the JTree Node
JSF : Component Binding Vs find component in the tree
Stopping a Webservice deployed on JBOSS
Change Component Style on Validation
Thread Boost feature
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:30:13.