Is it possible to have "movable"/"draggable" components like JButtons, JTextFields in a JFrame?
Basically I plan to place some buttons, textfields, labels, etc. on a JFrame and I would like to make it possible that a user can move the different components around on that JFrame with the mouse.
I have seen various ways with MouseListeners, subclassed JComponent code, DropSource/Dr开发者_如何转开发opTarget implementations and so on, but I'm unsure which is the "recommended" way (I don't need to support "drag and drop" between different Frames/Applications which is what most examples seem to do).
The Component Mover can do this for you.
Use the GlassPane: http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html
It's an invisible panel that sits on top of all other components. You can attach mouse listeners to it and then use SwingUtilities.getDeepestComponentAt() to figure out which component was clicked on beneath the GlassPane. Then use a mouseDragged listener on the glasspane and set the component location based on the mouse dragged event.
You will need to set the layout of your container to "null" so the components' setLocation will work.
精彩评论