TrayIcon
Aus Programmers Guide
import java.awt.AWTException;
import java.awt.Frame;
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.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Icon {
private static TrayIcon trayIcon;
private SystemTray tray;
private Image image;
private MouseListener mouseListener;
private String info = null;
private String titelhover = "Titel";
private String latestinfo = "";
private PopupMenu popup;
private MenuItem openItem, hideItem, tickItem, smalltickItem, bildItem,
statsItem, coverentryItem, browserItem, copyItem, coverItem,
overItem, defaultItem, homepageItem, realItem,googlesearchItem;
/**
* Machicon.
*/
public Icon() {
if (SystemTray.isSupported()) {
tray = SystemTray.getSystemTray();
image = Toolkit.getDefaultToolkit().getImage(
Icon.class.getResource(MainApplication.getLogoString()));
mouseListener = new MouseListener() {
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
};
popup = new PopupMenu();
popup.setName("Menue");
popup.setFont(MainApplication.getSchrift());
openItem = new MenuItem("TitelAnzeige öffnen");
popup.add(openItem);
trayIcon = new TrayIcon(image, titelhover, popup);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (Window.getF().getState() == 1) {
Window.getF().setVisible(true);
Window.getF().setState(Frame.NORMAL);
Window.getF().setVisible(true);
} else if (Window.getF().getState() == 0) {
Window.getF().setState(Frame.ICONIFIED);
Window.getF().setVisible(false);
}
}
};
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(actionListener);
trayIcon.addMouseListener(mouseListener);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err
.println("TrayIcon kann nicht hinzugefuegt werden...");
}
} else {
// System Tray is not supported
System.out
.println("Leider unterstützt Ihre Java-Version das Systemtray nicht, bitte updaten sie diese.");
}
}
public static TrayIcon getTrayIcon() {
return trayIcon;
}
public void setTitelhover() {
trayIcon.setToolTip(new GetTitel(1).getActualTitel());
}
public void setInfoString(){
info = new GetTitel(1).getExtraInformation();
if(!info.equalsIgnoreCase("") && !latestinfo.equalsIgnoreCase(info) && !info.isEmpty() && info != null){
trayIcon.displayMessage("Information",new GetTitel(1).getExtraInformation(),TrayIcon.MessageType.INFO);
latestinfo = info;
}
}
public static void message(String msg){
trayIcon.displayMessage("Information",msg,TrayIcon.MessageType.INFO);
}
}