Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

45
LINES

< > BotCompany Repo | #1035240 // ComponentDragger - drag a Component around when clicking on it (or on a subcomponent)

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (8084L) is out of date.

srecord noeq ComponentDragger extends MouseAdapter {
  Component component;
  MouseEvent mouseDownEvent;
  Component clickedOn;
  Pt rel;
  settable bool debug;
  
  // bring component to front when clicked?
  settable bool bringToFront;

  *(Component *component, MouseEvent *mouseDownEvent) {}
  
  void start {
    if (mouseDownEvent == null) ret;
    clickedOn = mouseDownEvent.getComponent();
    if (bringToFront)
      componentToFront(component);

    rel = pt(
      mouseDownEvent.getXOnScreen()-component.getX(),
      mouseDownEvent.getYOnScreen()-component.getY());
      
    addMouseAndMotionListener(clickedOn, this);
  }

  public void mouseDragged(MouseEvent e) {
    updatePosition(e);
  }
  
  public void mouseReleased(MouseEvent e) {
    if (e.getButton() != mouseDownEvent.getButton()) ret;
    updatePosition(e);
    removeMouseAdapter(clickedOn, this);
    if (debug) printVars("ComponentDragger done");
    dragDone();
  }
  
  void updatePosition(MouseEvent e) {
    int x = e.getXOnScreen()-rel.x, y = e.getYOnScreen()-rel.y;
    if (debug) printVars("ComponentDragger", +rel, +x, +y);
    component.setLocation(x, y);
  }
  
  void dragDone {}
}

Author comment

Began life as a copy of #1033463

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035240
Snippet name: ComponentDragger - drag a Component around when clicking on it (or on a subcomponent)
Eternal ID of this version: #1035240/11
Text MD5: 6f3186c700183c13f462afecd269e322
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-14 03:18:20
Source code size: 1218 bytes / 45 lines
Pitched / IR pitched: No / No
Views / Downloads: 27 / 80
Version history: 10 change(s)
Referenced in: [show references]