Drag component to create another one

杀马特。学长 韩版系。学妹 提交于 2019-12-13 01:27:36

问题


In all major Java IDEs, there is a GUI designer. When we select a component (A Jbutton, for example) and move it to a JPanel or JFrame, how is it done?
Is it a copy of the dragged component that is created on the other container?

On a project I'm working on, I have some JButton I would like to be able to drag to a panel. Theses JButton represent some actions, like "copy file", "move file", etc...
When one of those JButton is dragged, some options of the action will be displayed.

I checked TransferHandler but I don't know if it's the way to go. Is it?


回答1:


It's certainly possible. You'll need to study the Drag and Drop tutorial. In particular, you may want to implement Drop Location Rendering, discussed here, to symbolize the action.

By encapsulating a button's name, icon, listener, etc. in an Action instance, your importData() implementation can easily use setAction() to change the target button's behavior dynamically.

An alternative approach might be to add your buttons to a JToolBar. In normal mode, clicking the button evokes the Action; in editor mode , clicking the button changes the Action, again via setAction(), to one chosen from a list.



来源:https://stackoverflow.com/questions/13311150/drag-component-to-create-another-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!