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

57
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 (9230L) 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) {
      print("Bringing to front: " + component);
      componentToFront(component);
      repaint(getParent(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;
    
    // safety
    x = max(x, 0);
    y = max(y, 0);
    
    updatePosition(x, y);
  }
  
  swappable void updatePosition(int x, int 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/15
Text MD5: 05e694f8b09c4d3b189d2c07d9d50f18
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-30 17:13:11
Source code size: 1466 bytes / 57 lines
Pitched / IR pitched: No / No
Views / Downloads: 170 / 286
Version history: 14 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)