Search code examples
jtextpanejcomponent

How can i move a jpanel inside jtextpane, similar to dragging text from a location to another


It is required to move an image in textpane, image is inserted in jpanel, which is inside textarea. I want to move image by dragging mouse over it. How to move image into the text written so that text written in text pane rearrange itself around the image.


Solution

  • You need to use modelToView and viewToModel to convert the mouse location to a location in the document. If you want to see the component moving into the document, you might alternatively remove it and add it to its new location. Otherwise, just move the caret while dragging, and add the component during release.