Creates a listbox whose contents can be reordered by clicking and dragging items. The resulting listbox can be fully customized using standard GET and SET methods.
The listbox created is a standard Matlab UICONTROL('Style','Listbox'), whose underlying Java JList has the following properties modified: 'DragEnabled', 'DragSelectionEnabled', 'DropMode', 'DropTarget', 'MousePressedCallback', and 'MouseReleasedCallback'.
The user may specify their own 'MousePressedCallback' (JList), 'MouseReleasedCallback' (JList), 'DragOverCallback' (DropTarget), and 'DropCallback' (DropTarget) in the list of input property/value pairs. These callbacks will be called at the end of the REORDERABLELISTBOX default callbacks, with two inputs, HANDLE and EVENTDATA. The 'DragOverCallback' is given a third input, PERMORDER, which indicates how the list has been reordered, i.e. STRING = STRING(PERMORDER). This can be used to reorder any underlying data that the list may represent.
To reorder items, REORDERABLELISTBOX creates a drag-and-drop object tied to the listbox. An unintended result is that list items can be dragged off of the list and dropped onto other components. Items can also be dragged from other objects onto the list; dropping onto the list has no effect.
If the listbox displays erratic redraw behaviour when being reordered rapidly (single cells being drawn too tall or too wide), you may need to explicitly set the 'FixedCellHeight' and 'FixedCellWidth' properties of JLISTBOX (values of -1 tell Java to automatically determine cell size, but may lead to this erratic behaviour).
This utility makes use of the freely available FINDJOBJ, written by Yair Altman and available on the Matlab File Exchange:
Erik Koopmans (2020). Reorderable Listbox (https://www.mathworks.com/matlabcentral/fileexchange/37642-reorderable-listbox), MATLAB Central File Exchange. Retrieved .
Looks like a very nice utility!
I'm trying to learn how to use it, beyond the pure basics, and wonder if you have some kind of user manual or more examples in addition to the 5 basic examples inside the function. Specifically I'm trying to add elements to the list by drag&drop strings or files from windows, i.e. trying to understand how to work with the 'DropCallback' property, and trying to figure out how to work with the 'MouseReleasedCallback' property, and know which list element was clicked when this callback is called.
Any help would be appreciated!
A great idea, but it seems to be broken in R2017b
Index exceeds matrix dimensions.
Error in reorderableListbox (line 128)
jScrollPane = jScrollPane(1);
This also works with hg2 (tested on R2015b). @ClaytonChu - could you upload the class version somewhere?
Nice utility! Would be better if I could set the callbacks outside of the constructor. I modified this slightly to make it a class to do that and it works quite nicely.
Hi Soren, thanks! DND between listboxes would be useful - I'll look into it.
It would technically be possible with the current version of this function, by specifying your own DropCallback as an input (one that inserts the eventData.Transferable data into the list at the cursor location, if the source DropTarget is not the same as the destination). But you would run into at least one bug - the reorderableListbox still thinks it's in 'reorder' mode if you drop on something other than the listbox itself. With DND between listboxes, you'd be bound to run into this problem, and you'd be reordering the list you're trying to drop onto.
Earlier I decided this was an acceptable bug in reorderableListbox, but it would be nice to fix it anyway. In the process I'll try to build in the DND behaviour too. No promises that it'll happen soon, but I'll see what I can do!
Nice utility that works fine on my R2012b.
Is there any way to implement drag'n drop in between different listboxes?
works great. enables drag and drop to other applications as well.
1.1.1: Separated DropCallback from MouseReleasedCallback, for more user customization.
1.1.0: Added support for user-definable callbacks (MousePressed, MouseReleased, DragOver); added permOrder.
Modified description, documentation, and screenshot.
Inspired: Simulink Subsystem Port Organiser