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

67
LINES

< > BotCompany Repo | #1034105 // CustomToolTip

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

Libraryless. Click here for Pure Java version (6703L/37K).

// ToolTip mechanism with custom Swing content.
// Note: Doesn't make a border by itself, just shows the raw component
// returned by makeContent.

sclass CustomToolTip is AutoCloseable {
  JComponent target;
  JWindow window;
  bool debug;
  MouseAdapter mouseAdapter;

  swappable JComponent makeContent() { null; }
  
  *(IF0<JComponent> *makeContent) {}
  
  // run in Swing thread
  void move {
    if (window == null) ret;
    if (debug) print("CustomToolTip move");
    var mouse = mouseLocationOnScreen();
    window.setLocation(mouse.x+10, mouse.y+10);
  }
  
  // run in Swing thread
  void showOrMove {
    if (window != null)
      ret with move();

    window = new JWindow;
    window.pack();

    thread "Make tool tip content" {
      var content = makeContent();
      if (content == null) ret;
      swing {
        setWindowContents(window, content);
        move();
        window.pack();
        if (isAlwaysOnTop(getWindow(target)))
          window.setAlwaysOnTop(true);
        showWindow(window);
      }
    }
  }
  
  void hide swing {
    disposeWindow(window);
    window = null;
  }
  
  void installOn(JComponent target) {
    this.target = target;
    if (target == null) ret;
    
    addMouseAdapter(target, mouseAdapter = new MouseAdapter {
      @Override public void mouseEntered aka mouseMoved(MouseEvent e) { showOrMove(); }
      @Override public void mouseExited aka mousePressed(MouseEvent e) { hide(); }
    });
    
    target.setToolTipText(null);
  }
  
  public void close swing {
    hide();
    removeMouseAdapter(target, mouseAdapter);
    target = null;
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1034105
Snippet name: CustomToolTip
Eternal ID of this version: #1034105/12
Text MD5: fecda30462fbb17179e322217bfc1c9e
Transpilation MD5: ef83da9dac3ddc812ae17ccb99bfd4a4
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-22 17:10:09
Source code size: 1676 bytes / 67 lines
Pitched / IR pitched: No / No
Views / Downloads: 133 / 279
Version history: 11 change(s)
Referenced in: [show references]