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.

1  
srecord noeq ComponentDragger extends MouseAdapter {
2  
  Component component;
3  
  MouseEvent mouseDownEvent;
4  
  Component clickedOn;
5  
  Pt rel;
6  
  settable bool debug;
7  
  
8  
  // bring component to front when clicked?
9  
  settable bool bringToFront;
10  
11  
  *(Component *component, MouseEvent *mouseDownEvent) {}
12  
  
13  
  void start {
14  
    if (mouseDownEvent == null) ret;
15  
    clickedOn = mouseDownEvent.getComponent();
16  
    if (bringToFront) {
17  
      print("Bringing to front: " + component);
18  
      componentToFront(component);
19  
      repaint(getParent(component));
20  
    }
21  
22  
    rel = pt(
23  
      mouseDownEvent.getXOnScreen()-component.getX(),
24  
      mouseDownEvent.getYOnScreen()-component.getY());
25  
      
26  
    addMouseAndMotionListener(clickedOn, this);
27  
  }
28  
29  
  public void mouseDragged(MouseEvent e) {
30  
    updatePosition(e);
31  
  }
32  
  
33  
  public void mouseReleased(MouseEvent e) {
34  
    if (e.getButton() != mouseDownEvent.getButton()) ret;
35  
    updatePosition(e);
36  
    removeMouseAdapter(clickedOn, this);
37  
    if (debug) printVars("ComponentDragger done");
38  
    dragDone();
39  
  }
40  
  
41  
  void updatePosition(MouseEvent e) {
42  
    int x = e.getXOnScreen()-rel.x, y = e.getYOnScreen()-rel.y;
43  
    
44  
    // safety
45  
    x = max(x, 0);
46  
    y = max(y, 0);
47  
    
48  
    updatePosition(x, y);
49  
  }
50  
  
51  
  swappable void updatePosition(int x, int y) {
52  
    if (debug) printVars("ComponentDragger", +rel, +x, +y);
53  
    component.setLocation(x, y);
54  
  }
55  
  
56  
  void dragDone {}
57  
}

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: 171 / 289
Version history: 14 change(s)
Referenced in: [show references]