问题
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