Libraryless. Click here for Pure Java version (6703L/37K).
1 | // ToolTip mechanism with custom Swing content. |
2 | // Note: Doesn't make a border by itself, just shows the raw component |
3 | // returned by makeContent. |
4 | |
5 | sclass CustomToolTip is AutoCloseable { |
6 | JComponent target; |
7 | JWindow window; |
8 | bool debug; |
9 | MouseAdapter mouseAdapter; |
10 | |
11 | swappable JComponent makeContent() { null; } |
12 | |
13 | *(IF0<JComponent> *makeContent) {} |
14 | |
15 | // run in Swing thread |
16 | void move { |
17 | if (window == null) ret; |
18 | if (debug) print("CustomToolTip move"); |
19 | var mouse = mouseLocationOnScreen(); |
20 | window.setLocation(mouse.x+10, mouse.y+10); |
21 | } |
22 | |
23 | // run in Swing thread |
24 | void showOrMove { |
25 | if (window != null) |
26 | ret with move(); |
27 | |
28 | window = new JWindow; |
29 | window.pack(); |
30 | |
31 | thread "Make tool tip content" { |
32 | var content = makeContent(); |
33 | if (content == null) ret; |
34 | swing { |
35 | setWindowContents(window, content); |
36 | move(); |
37 | window.pack(); |
38 | if (isAlwaysOnTop(getWindow(target))) |
39 | window.setAlwaysOnTop(true); |
40 | showWindow(window); |
41 | } |
42 | } |
43 | } |
44 | |
45 | void hide swing { |
46 | disposeWindow(window); |
47 | window = null; |
48 | } |
49 | |
50 | void installOn(JComponent target) { |
51 | this.target = target; |
52 | if (target == null) ret; |
53 | |
54 | addMouseAdapter(target, mouseAdapter = new MouseAdapter { |
55 | @Override public void mouseEntered aka mouseMoved(MouseEvent e) { showOrMove(); } |
56 | @Override public void mouseExited aka mousePressed(MouseEvent e) { hide(); } |
57 | }); |
58 | |
59 | target.setToolTipText(null); |
60 | } |
61 | |
62 | public void close swing { |
63 | hide(); |
64 | removeMouseAdapter(target, mouseAdapter); |
65 | target = null; |
66 | } |
67 | } |
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: | 134 / 281 |
Version history: | 11 change(s) |
Referenced in: | [show references] |