Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and Drop in GUI

Is it possible to create an object in a GUI whose position I can define by the cursor position (drag when clicked) by setting its 'Position' property to whatever the cursor position is? What function should I be using?

like image 511
straits Avatar asked Jun 23 '11 10:06

straits


People also ask

What is drag and drop in Java?

Drag and Drop enables data transfer across Java programming language and native applications, between Java programming language applications, and within a single Java programming language application.

Is there drag and drop in Python?

Drag and Drop refers to moving widget while holding left click pressed. One can drag the widget or object in the x-axis or y-axis. As per the official documentation to enable an object to be dragged, It is necessary to bind an event to a callback function.

What is swing in GUI?

Swing in Java is a lightweight GUI toolkit which has a wide variety of widgets for building optimized window based applications. It is a part of the JFC( Java Foundation Classes). It is build on top of the AWT API and entirely written in java. It is platform independent unlike AWT and has lightweight components.


1 Answers

You can use the SELECTMOVERESIZE function to turn on moving and resizing for your GUI object. Then you can just click and drag the object with the mouse. It's as simple as this:

set(hObject,'ButtonDownFcn','selectmoveresize');

What's not so simple is if your GUI object is a uicontrol object, in which case you will have to disable the object by setting the 'Enable' property to 'off' or 'inactive' in order to have the 'ButtonDownFcn' function execute instead of the 'Callback' function. This is true even if you haven't defined a callback for the object.

You will also probably need to add a means to your GUI to turn the moving and resizing of the object on and off, perhaps an extra button or a menu item you can select. To show how you could do this with a push button, here's a simple example that creates a figure with an editable text box and a push button that turns on and off the ability to move and resize the editable text box:

function GUI_example

  hFigure = figure('Position',[100 100 200 200],...  %# Create a figure
                   'MenuBar','none',...
                   'ToolBar','none');
  hEdit = uicontrol('Style','edit',...               %# Create a multi-line
                    'Parent',hFigure,...             %#   editable text box
                    'Position',[10 30 180 160],...
                    'Max',2,...
                    'String',{'(type here)'});
  hButton = uicontrol('Style','pushbutton',...       %# Create a push button
                      'Parent',hFigure,...
                      'Position',[50 5 100 20],...
                      'String','Turn moving on',...
                      'Callback',@button_callback);

  function button_callback(hSource,eventData)        %# Nested button callback

    if strcmp(get(hSource,'String'),'Turn moving on')
      set(hSource,'String','Turn moving off');          %# Change button text
      set(hEdit,'Enable','inactive',...                 %# Disable the callback
                'ButtonDownFcn','selectmoveresize',...  %# Turn on moving, etc.
                'Selected','on');                       %# Display as selected
    else
      set(hSource,'String','Turn moving on');           %# Change button text
      set(hEdit,'Enable','on',...                       %# Re-enable the callback
                'ButtonDownFcn','',...                  %# Turn off moving, etc.
                'Selected','off');                      %# Display as unselected
    end

  end

end

Note: although the documentation lists the 'Selected' property as read-only, I was able to modify it without a problem. It must be a typo in the documentation.

like image 127
gnovice Avatar answered Oct 01 '22 15:10

gnovice