Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

d'n'd any component  RSS feed

 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • 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
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!