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

39
LINES

< > BotCompany Repo | #1033463 // WindowDragger - drag da window around [for undecorated JFrames, OK]

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

Libraryless. Click here for Pure Java version (5120L/28K).

sclass WindowDragger extends MouseAdapter {
  MouseEvent mouseDownEvent;
  JComponent src;
  Pt rel;
  Window window;
  
  *(MouseEvent *mouseDownEvent) {
    if (mouseDownEvent == null) ret;
    src = optCast JComponent(mouseDownEvent.getComponent());
    if (src == null) ret;
    window = getWindow(src);
    if (window == null) ret;
    
    rel = pt(
      mouseDownEvent.getXOnScreen()-window.getX(),
      mouseDownEvent.getYOnScreen()-window.getY());
      
    addMouseAndMotionListener(src, this);
  }

  public void mouseDragged(MouseEvent e) {
    updatePosition(e);
  }
  
  public void mouseReleased(MouseEvent e) {
    if (e.getButton() != mouseDownEvent.getButton()) ret;
    updatePosition(e);
    removeMouseAdapter(src, this);
    dragDone();
  }
  
  void updatePosition(MouseEvent e) {
    window.setLocation(
      e.getXOnScreen()-rel.x,
      e.getYOnScreen()-rel.y);
  }
  
  void dragDone {}
}

Author comment

Began life as a copy of #1018716

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1033463
Snippet name: WindowDragger - drag da window around [for undecorated JFrames, OK]
Eternal ID of this version: #1033463/13
Text MD5: 08408e3b6ee184ca5d010e6f36eba2d9
Transpilation MD5: a16fe23f483afa00480aa94d7ac778c4
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-29 00:51:22
Source code size: 957 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 196 / 329
Version history: 12 change(s)
Referenced in: [show references]