Java/JDK 6/TrayIcon — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 06:12, 1 июня 2010
Содержание
Add mouse listener to TrayIcon
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
public class SystemTrayDemo2 {
public static void main(String[] args) {
if (!SystemTray.isSupported()) {
return;
}
SystemTray tray = SystemTray.getSystemTray();
Dimension size = tray.getTrayIconSize();
BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.setColor(Color.blue);
g.fillRect(0, 0, size.width, size.height);
g.setColor(Color.yellow);
int ovalSize = (size.width < size.height) ? size.width : size.height;
ovalSize /= 2;
g.fillOval(size.width / 4, size.height / 4, ovalSize, ovalSize);
try {
PopupMenu popup = new PopupMenu();
MenuItem miExit = new MenuItem("Exit");
ActionListener al;
al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Goodbye");
System.exit(0);
}
};
miExit.addActionListener(al);
popup.add(miExit);
TrayIcon ti = new TrayIcon(bi, "System Tray Demo #2", popup);
al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
};
ti.setActionCommand("My Icon");
ti.addActionListener(al);
MouseListener ml;
ml = new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.println("Tray icon: Mouse clicked");
}
public void mouseEntered(MouseEvent e) {
System.out.println("Tray icon: Mouse entered");
}
public void mouseExited(MouseEvent e) {
System.out.println("Tray icon: Mouse exited");
}
public void mousePressed(MouseEvent e) {
System.out.println("Tray icon: Mouse pressed");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Tray icon: Mouse released");
}
};
ti.addMouseListener(ml);
MouseMotionListener mml;
mml = new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
System.out.println("Tray icon: Mouse dragged");
}
public void mouseMoved(MouseEvent e) {
System.out.println("Tray icon: Mouse moved");
}
};
ti.addMouseMotionListener(mml);
tray.add(ti);
} catch (AWTException e) {
System.out.println(e.getMessage());
return;
}
}
}
Add Mouse motion listener to TrayIcon
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
public class SystemTrayDemo2 {
public static void main(String[] args) {
if (!SystemTray.isSupported()) {
return;
}
SystemTray tray = SystemTray.getSystemTray();
Dimension size = tray.getTrayIconSize();
BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.setColor(Color.blue);
g.fillRect(0, 0, size.width, size.height);
g.setColor(Color.yellow);
int ovalSize = (size.width < size.height) ? size.width : size.height;
ovalSize /= 2;
g.fillOval(size.width / 4, size.height / 4, ovalSize, ovalSize);
try {
PopupMenu popup = new PopupMenu();
MenuItem miExit = new MenuItem("Exit");
ActionListener al;
al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Goodbye");
System.exit(0);
}
};
miExit.addActionListener(al);
popup.add(miExit);
TrayIcon ti = new TrayIcon(bi, "System Tray Demo #2", popup);
al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
};
ti.setActionCommand("My Icon");
ti.addActionListener(al);
MouseListener ml;
ml = new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.println("Tray icon: Mouse clicked");
}
public void mouseEntered(MouseEvent e) {
System.out.println("Tray icon: Mouse entered");
}
public void mouseExited(MouseEvent e) {
System.out.println("Tray icon: Mouse exited");
}
public void mousePressed(MouseEvent e) {
System.out.println("Tray icon: Mouse pressed");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Tray icon: Mouse released");
}
};
ti.addMouseListener(ml);
MouseMotionListener mml;
mml = new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
System.out.println("Tray icon: Mouse dragged");
}
public void mouseMoved(MouseEvent e) {
System.out.println("Tray icon: Mouse moved");
}
};
ti.addMouseMotionListener(mml);
tray.add(ti);
} catch (AWTException e) {
System.out.println(e.getMessage());
return;
}
}
}
Add PropertyChangeListener to TrayIcon
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class SystemTrayDemo1 {
public static void main(String[] args) throws Exception {
if (!SystemTray.isSupported()) {
return;
}
SystemTray tray = SystemTray.getSystemTray();
PropertyChangeListener pcl;
pcl = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent pce) {
System.out.println("Property changed = " + pce.getPropertyName());
TrayIcon[] tia = (TrayIcon[]) pce.getOldValue();
if (tia != null) {
System.out.println("Old tray icon array contents: ");
for (int i = 0; i < tia.length; i++)
System.out.println(tia[i]);
System.out.println();
}
tia = (TrayIcon[]) pce.getNewValue();
if (tia != null) {
System.out.println("New tray icon array contents: ");
for (int i = 0; i < tia.length; i++)
System.out.println(tia[i]);
System.out.println();
}
}
};
tray.addPropertyChangeListener("trayIcons", pcl);
Dimension size = tray.getTrayIconSize();
BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.setColor(Color.blue);
g.fillRect(0, 0, size.width, size.height);
g.setColor(Color.yellow);
int ovalSize = (size.width < size.height) ? size.width : size.height;
ovalSize /= 2;
g.fillOval(size.width / 4, size.height / 4, ovalSize, ovalSize);
TrayIcon icon = null;
tray.add(icon = new TrayIcon(bi));
Thread.sleep(3000);
tray.remove(icon);
Thread.sleep(3000);
System.exit(0);
}
}
Create Tray Icon
import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test {
static Image image = Toolkit.getDefaultToolkit().getImage("images/tray.gif");
static TrayIcon trayIcon = new TrayIcon(image, "Tester2");
public static void main(String[] a) throws Exception {
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("In here");
trayIcon.displayMessage("Tester!", "Some action performed", TrayIcon.MessageType.INFO);
}
});
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println("TrayIcon could not be added.");
}
}
}
}
Demonstrating a System Tray That Responds to Selection
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class ActiveTray {
public static void main(String args[]) throws Exception{
if (SystemTray.isSupported() == false) {
System.err.println("No system tray available");
return;
}
final SystemTray tray = SystemTray.getSystemTray();
PropertyChangeListener propListener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
TrayIcon oldTray[] = (TrayIcon[]) evt.getOldValue();
TrayIcon newTray[] = (TrayIcon[]) evt.getNewValue();
System.out.println(oldTray.length + " / " + newTray.length);
}
};
tray.addPropertyChangeListener("trayIcons", propListener);
Image image = Toolkit.getDefaultToolkit().getImage("jpgIcon.jpg");
PopupMenu popup = new PopupMenu();
MenuItem item = new MenuItem("Hello, World");
final TrayIcon trayIcon = new TrayIcon(image, "Tip Text", popup);
ActionListener menuActionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage("Good-bye", "Cruel World", TrayIcon.MessageType.WARNING);
}
};
item.addActionListener(menuActionListener);
popup.add(item);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
tray.remove(trayIcon);
}
};
trayIcon.addActionListener(actionListener);
tray.add(trayIcon);
}
}
Have a systray icon (Windows)
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class Main {
static TrayIcon trayIcon = null;
public static void main(String[] args) throws Exception {
SystemTray sysTray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("/a.jpg");
ActionListener exitListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Bye from the tray");
System.exit(0);
}
};
ActionListener executeListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "!", "User action",
JOptionPane.INFORMATION_MESSAGE);
trayIcon.displayMessage("Done", "!", TrayIcon.MessageType.INFO);
}
};
PopupMenu menu = new PopupMenu();
MenuItem execItem = new MenuItem("Action...");
execItem.addActionListener(executeListener);
menu.add(execItem);
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(exitListener);
menu.add(exitItem);
trayIcon = new TrayIcon(image, "Java System Tray Demo", menu);
trayIcon.setImageAutoSize(true);
sysTray.add(trayIcon);
trayIcon.displayMessage("Ready", "started", TrayIcon.MessageType.INFO);
}
}
Remove Icon from TrayIcon
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class SystemTrayDemo1 {
public static void main(String[] args) throws Exception {
if (!SystemTray.isSupported()) {
return;
}
SystemTray tray = SystemTray.getSystemTray();
PropertyChangeListener pcl;
pcl = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent pce) {
System.out.println("Property changed = " + pce.getPropertyName());
TrayIcon[] tia = (TrayIcon[]) pce.getOldValue();
if (tia != null) {
System.out.println("Old tray icon array contents: ");
for (int i = 0; i < tia.length; i++)
System.out.println(tia[i]);
System.out.println();
}
tia = (TrayIcon[]) pce.getNewValue();
if (tia != null) {
System.out.println("New tray icon array contents: ");
for (int i = 0; i < tia.length; i++)
System.out.println(tia[i]);
System.out.println();
}
}
};
tray.addPropertyChangeListener("trayIcons", pcl);
Dimension size = tray.getTrayIconSize();
BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.setColor(Color.blue);
g.fillRect(0, 0, size.width, size.height);
g.setColor(Color.yellow);
int ovalSize = (size.width < size.height) ? size.width : size.height;
ovalSize /= 2;
g.fillOval(size.width / 4, size.height / 4, ovalSize, ovalSize);
TrayIcon icon = null;
tray.add(icon = new TrayIcon(bi));
Thread.sleep(3000);
tray.remove(icon);
Thread.sleep(3000);
System.exit(0);
}
}
System tray icon
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.JOptionPane;
import javax.swing.plaf.metal.MetalIconFactory;
public class SysTray {
public static void main(String[] args) throws Exception {
new SysTray().installSystemTray();
}
public void installSystemTray() throws Exception {
PopupMenu menu = new PopupMenu();
MenuItem exit = new MenuItem("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(exit);
TrayIcon icon = new TrayIcon(getImage(), "Java application as a tray icon", menu);
icon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hi!");
}
});
SystemTray.getSystemTray().add(icon);
Thread.sleep(3000);
icon.displayMessage("Attention", "Please click here", TrayIcon.MessageType.WARNING);
}
private Image getImage() throws HeadlessException {
Icon defaultIcon = MetalIconFactory.getTreeHardDriveIcon();
Image img = new BufferedImage(defaultIcon.getIconWidth(), defaultIcon.getIconHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);
return img;
}
}
Using system tray
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class SystemTrayDemo {
public static void main(String[] args)throws Exception {
if (!SystemTray.isSupported()) {
System.out.println("SystemTray is not supported");
return;
}
SystemTray tray = SystemTray.getSystemTray();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("trayIcon.jpg");
PopupMenu menu = new PopupMenu();
MenuItem messageItem = new MenuItem("Show Message");
messageItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "www.jexp.ru");
}
});
menu.add(messageItem);
MenuItem closeItem = new MenuItem("Close");
closeItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(closeItem);
TrayIcon icon = new TrayIcon(image, "SystemTray Demo", menu);
icon.setImageAutoSize(true);
tray.add(icon);
}
}