5720

Drag component to create another one

Question:

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?<br /> 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...<br /> When one of those JButton is dragged, some options of the action will be displayed.

I checked <a href="http://docs.oracle.com/javase/6/docs/api/javax/swing/TransferHandler.html" rel="nofollow">TransferHandler</a> but I don't know if it's the way to go. Is it?

Answer1:

It's certainly possible. You'll need to study the <a href="http://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html" rel="nofollow"><em>Drag and Drop</em></a> tutorial. In particular, you may want to implement <a href="http://docs.oracle.com/javase/tutorial/uiswing/dnd/droplocation.html" rel="nofollow"><em>Drop Location Rendering</em></a>, discussed <a href="https://forums.oracle.com/forums/thread.jspa?threadID=1350570" rel="nofollow">here</a>, to symbolize the action.

By encapsulating a button's name, icon, listener, etc. in an <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html" rel="nofollow">Action</a> 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 <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/toolbar.html" rel="nofollow">JToolBar</a>. In normal mode, clicking the button <em>evokes</em> the Action; in editor mode , clicking the button <em>changes</em> the Action, again via setAction(), to one chosen from a list.

Recommend

  • Service stack arrayof to be removed
  • How to free memory using Core Data External Storage?
  • User Authentication on a mobile AngularJS App
  • Applying functions to multidimensional numpy arrays without loops
  • C# How to retrieve a certain text located within a string?
  • Spring Data JPA - custom @Query with “@Param Date” doesn't work
  • How do i read EXIF data from an image without the use of external scripts in python?
  • JSplitPane + MiGLayout: how to enable autoresizing
  • Can weak symbol be resolved among libraries during linking?
  • Need clear idea about timespec structure
  • When to use clone() and how actually addAll() and add() works
  • JTextPane: KeyBindings are not working on StyledEditorKit
  • How can the java 'class' literal return different instances of the Class object for the sa
  • In BASH convert a string with . in float
  • JQuery UI selectable plugin - Multiple mouse drag selection and unselect option
  • Change color of row programmatically in WatchKit
  • NSOpenPanel's setDirectoryURL doesn't work on Lion
  • Windows Forms switch between Panels
  • Can't CTRL+Drag NSButton to custom NSView header
  • Add Jpanel to Jframe NetBeans
  • jquery draggable stop event
  • Extjs, handling success or failure when doing a standard submit in a form
  • setSelected() with JRadioButton r[]=new JRadioButton[3] not working [duplicate]
  • Is there a way to link a linux's thread TID and a pthread_t “thread ID”
  • Basic defensive programming [duplicate]
  • Pythons argparse default value doesn't work
  • Fully customized Python Help Usage
  • sweetalert2 inputoptions from file in select example
  • Switch to popup in python using selenium
  • How to disable all widgets inside Panel or inside Composite?
  • Change JButton Shape while respecting Look And Feel
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Comma separated Values
  • Matrix multiplication with MKL
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Hits per day in Google Big Query
  • coudnt use logback because of log4j
  • File not found error Google Drive API
  • Converting MP3 duration time