-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip_in_menu.rb
More file actions
37 lines (28 loc) · 1003 Bytes
/
Copy pathip_in_menu.rb
File metadata and controls
37 lines (28 loc) · 1003 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
include Java
import java.awt.TrayIcon
import java.awt.Toolkit
import java.net.InetAddress
import java.awt.datatransfer.StringSelection;
#Menu Items
exitItem = java.awt.MenuItem.new("Exit")
copyItem = java.awt.MenuItem.new("Copy")
ipAddress = java.net.InetAddress.getLocalHost().getHostAddress()
ipItem = java.awt.MenuItem.new(ipAddress)
def copyIpAddress(ipAddress)
clipboard = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard()
newContents = java.awt.datatransfer.StringSelection.new(ipAddress)
clipboard.setContents(newContents, nil)
end
# Event handling
exitItem.add_action_listener{ java.lang.System::exit(0) }
copyItem.add_action_listener { copyIpAddress(ipAddress) }
# PopUp Menu
popup = java.awt.PopupMenu.new
popup.add(ipItem)
popup.add(copyItem)
popup.add(exitItem)
image = java.awt.Toolkit::default_toolkit.get_image("ip-icon.png")
trayIcon = TrayIcon.new(image, ipAddress, popup)
trayIcon.image_auto_size = true
tray = java.awt.SystemTray::system_tray
tray.add(trayIcon)