Now, create tray icons on the Windows Taskbar with AWT. The java.awt.SystemTray and java.awt.TrayIcon are the classes that help you. The following example illustrates it.
tray=SystemTray.getSystemTray(): This is a method of the java.awt.SystemTray class which returns a java.awt.SystemTray object with which we can add tray icons to the system tray. You'll have to note that not all Operating systems support system tray.
t=new TrayIcon(new ImageIcon("C:\\Users\\Computer\\Downloads\\icon11.jpg").getImage()): This constructor in java.awt.TrayIcon class takes java.awt.Image as parameter which points out the image to be displayed as icon in the system tray. For this, i've used javax.swing.ImageIcon class which has a constructor that takes java.lang.String as parameter which represents the path of the image file. The javax.swing.ImageIcon consists of a method called getImage() which returns a java.awt.Image object pointing to the image specified (here the image is icon11.jpg and the path is specified above).
See how the code goes!
SystemTray in AWT - Explanation
jb=new JButton("Hide"): Create a new JButton with text Hide
When JButton is clicked..
tray=SystemTray.getSystemTray(): This is a method of the java.awt.SystemTray class which returns a java.awt.SystemTray object with which we can add tray icons to the system tray. You'll have to note that not all Operating systems support system tray.
t=new TrayIcon(new ImageIcon("C:\\Users\\Computer\\Downloads\\icon11.jpg").getImage()): This constructor in java.awt.TrayIcon class takes java.awt.Image as parameter which points out the image to be displayed as icon in the system tray. For this, i've used javax.swing.ImageIcon class which has a constructor that takes java.lang.String as parameter which represents the path of the image file. The javax.swing.ImageIcon consists of a method called getImage() which returns a java.awt.Image object pointing to the image specified (here the image is icon11.jpg and the path is specified above).
What if user clicks on Tray icon?
me.getButton(): This method returns the button which the user has clicked ex. left click or right click... MouseEvent.BUTTON1 represents left click.
setVisible(true): Make the frame visible. This is done when user left-clicks on the tray icon.
tray.remove(t): Remove tray icon. When frame is visible, i don't need a tray icon. That's why i've removed it.
p=new PopupMenu(): Create a popup menu, the menu that appears when user right-clicks on any component to which it is added.
System.exit(0): Exit the program when the user clicks on menu item exit
tray.remove(t): Remove icon from the tray, when the user clicks on menu item remove (which has text Hide Icon) on it.
setVisible(true): When icon is no more, the frame has to visible, else the user will suffer.
t.setPopupMenu(m): Set the popup menu named m which contains menu items exit,remove to the TrayIcon. (Right click on the tray icon to see this menu)
tray.add(t): Add the tray icon t to tray.
setVisible(false): When tray icon is visible, i don't need the frame to be visible. Make either one visible.
Also see..
External links that may help you..
setVisible(false): When tray icon is visible, i don't need the frame to be visible. Make either one visible.
Also see..
No comments:
Post a Comment