Class ReorderListTransferHandler

java.lang.Object
javax.swing.TransferHandler
org.bzdev.swing.ReorderListTransferHandler
All Implemented Interfaces:
Serializable

public class ReorderListTransferHandler extends TransferHandler
JList TransferHandler supporting reordering. This transfer handler allows a JList to be reordered using drag and drop operations. It does not allow D&D to be used to add or remove elements from a list. One use case this transfer handler supports is a component that contains a JList, a button to add new entries to the JList and name them, and some additional parameters. The use of this transfer handler then allows the JList to be reordered conveniently.

Typically, a list using this transfer handler has a scroll pane as a parent. In the following, we assume that the list contains elements whose type is OurType and that the class OurType has a toString() method that will produce a string whose length is the largest string length that should appear in a JList for OurType. The code is straightforward:


  DefaultListModel<OurType> listModel = new DefaultListModel<OurType>();
  JList<OurType> list = new JList<OurType>(listModel);
  JScrollPane scrollPane = new JScrollPane(list);
  TransferHandler th = new ReorderListTransferHandler(list);
  list.setPrototypeCellValue(new OurType(...));
  list.setTranferHandler(th);
  list.setDragEnabled(true);
  list.setDropMode(DropMode.INSERT);
 
It is basically the default sequence of operations for enabling drag and drop for a JList.
See Also: