• 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

Drag and drop images

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am looking for a good way to drag and drop images with (changeable) text on them across a panel.
Would there be any way to drag JPanel's around the screen with the mouse and have them stay on the spot they were released? (not snap into something like a gridlayout)
The panels should be fully visible while dragged. So I don't think I can use the drag gable interface.
Currently I am just using an extended JPanel but I am only now implementing the changeable text and JPannels give a lot of flexibility to add more JLabels later.
Would it be possible to make these draggable Panels and should I start looking in extending a JPanel or a layout?
Thank you for any help!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a link to the start of Oracle's tutorial about drag-and-drop: Introduction to DnD. (Acquired via Google keywords "java drag and drop tutorial".) It goes on for about 20 pages; it isn't a simple thing so don't expect to have working code within the hour!
 
Tim Sangster
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Here's a link to the start of Oracle's tutorial about drag-and-drop: Introduction to DnD. (Acquired via Google keywords "java drag and drop tutorial".) It goes on for about 20 pages; it isn't a simple thing so don't expect to have working code within the hour!


Thank you for the reply, however I looked into this and it does not seem to be the kind of drag and drop I am looking for.
I need the kind of drag and drop you find in uml tool's, you press the mouse on a button the image and an image appears under the mouse, you then drag the image to where you want it and release it there.
I am sorry if this is also included in the Oracle dnd demo but I overlooked it.
Are there any existing interfaces for that kind of dnd?
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your question is contradictory. Initially, you mentioned about dragging a JPanel - then, you say you need something like that in UML tools.
I think what you want is to actually 'drop' something into a JPanel. So, in that case, you should propably have a JLabel or a JToggleButton with an image on it. Then, you would drag this component and drop it into the JPanel.

The examples given in the tutorial use StringSelection which is to transfer String data - so, in your case you should write a class that implements Transferable - this class should support DataFlavor.imageFlavor and the getTransferData method should return an Image object. Not an easy one to write though.

Finally, based on what to do on the 'drop' action - if you just want to add a JLabel to the panel, then as you have mentioned, it would become dependant on the layout used. If you use a null layout, then you can add the label at the drop location. If you want to draw the dragged image, then, you can use the repaint(x,y,w,h) method to signify where to draw and then draw the actual image in the paintComponent method.
 
Tim Sangster
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ranganathan Kaliyur Mannar wrote:Your question is contradictory. Initially, you mentioned about dragging a JPanel - then, you say you need something like that in UML tools.
I think what you want is to actually 'drop' something into a JPanel. So, in that case, you should propably have a JLabel or a JToggleButton with an image on it. Then, you would drag this component and drop it into the JPanel.

The examples given in the tutorial use StringSelection which is to transfer String data - so, in your case you should write a class that implements Transferable - this class should support DataFlavor.imageFlavor and the getTransferData method should return an Image object. Not an easy one to write though.

Finally, based on what to do on the 'drop' action - if you just want to add a JLabel to the panel, then as you have mentioned, it would become dependant on the layout used. If you use a null layout, then you can add the label at the drop location. If you want to draw the dragged image, then, you can use the repaint(x,y,w,h) method to signify where to draw and then draw the actual image in the paintComponent method.


Thank you for the response, You'r right I mean drop not only drag. I'm trying to work with a self written layout now and if that doesn't work out I will just draw everything myself.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic