Created
July 13, 2011 17:07
-
-
Save ts-3156/1080761 to your computer and use it in GitHub Desktop.
タスクトレイにアイコンを表示する。http://sattontanabe.blog86.fc2.com/blog-entry-24.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main; | |
import java.awt.Frame; | |
import java.awt.TrayIcon; | |
import java.awt.event.MouseEvent; | |
import java.awt.event.MouseListener; | |
import java.awt.event.WindowEvent; | |
import java.awt.event.WindowListener; | |
/** | |
* 点滅されたアイコンがクリックされた時に、発生させるイベント Listenerクラス。 | |
*/ | |
public class ChangeLightImgListener implements WindowListener, MouseListener { | |
String lightImageText; | |
TaskTray taskTray; | |
Frame frame; | |
TrayIcon trayIcon; | |
public ChangeLightImgListener(String lightImageText, TaskTray taskTray, Frame frame, TrayIcon trayIcon) { | |
this.lightImageText = lightImageText; | |
this.taskTray = taskTray; | |
this.frame = frame; | |
this.trayIcon = trayIcon; | |
} | |
/** | |
* タスクトレイアイコンがクリックされた時に に ポップアップ・メッセージを表示する。 | |
*/ | |
public void mousePressed(MouseEvent e) { | |
taskTray.displayMessage(null, lightImageText); | |
} | |
/** | |
* 対象のアプリケーションがアクティブになった時に点滅した アイコンを戻す。 | |
*/ | |
public void windowActivated(WindowEvent e) { | |
taskTray.replaceImageWithDelete(frame.getIconImage()); | |
frame.removeWindowListener(this); | |
trayIcon.removeMouseListener(this); | |
} | |
public void windowClosed(WindowEvent e) { | |
} | |
public void windowClosing(WindowEvent e) { | |
} | |
public void windowDeactivated(WindowEvent e) { | |
} | |
public void windowDeiconified(WindowEvent e) { | |
} | |
public void windowIconified(WindowEvent e) { | |
} | |
public void windowOpened(WindowEvent e) { | |
} | |
public void mouseClicked(MouseEvent e) { | |
} | |
public void mouseEntered(MouseEvent e) { | |
} | |
public void mouseExited(MouseEvent e) { | |
} | |
public void mouseReleased(MouseEvent e) { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main; | |
import java.awt.AWTException; | |
import java.awt.Dimension; | |
import java.awt.FlowLayout; | |
import java.awt.Image; | |
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.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import java.awt.event.WindowAdapter; | |
import java.awt.event.WindowEvent; | |
import java.io.File; | |
import javax.imageio.ImageIO; | |
import javax.swing.ImageIcon; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.SwingUtilities; | |
import javax.swing.UIManager; | |
/** | |
* http://sattontanabe.blog86.fc2.com/blog-entry-24.html | |
* 上記のURLのサンプルを参考にしてちょっと作り替えただけです。 | |
* Javaでタスクトレイ(システムトレイ)のアイコンを表示・点滅。 | |
* | |
* ◆ このクラスの概要 TaskTrayクラスはタスクトレイを扱うユーティリティとして、 以下の機能を提供します。 ・タスクトレイにアイコンを表示。 | |
* ・タスクトレイにポップアップ・メッセージを表示。 ・タスクトレイを点滅させる。 | |
* | |
* ◆ 使い方 以下のメソッド引数に対象のJFrameクラスを渡します。 ・createTray(JFrame targetFrame) | |
* ・createTray(final JFrame targetFrame, Image image, PopupMenu menu) | |
* | |
*/ | |
public class TaskTray { | |
private static final SystemTray TRAY = SystemTray.getSystemTray(); | |
private static final Object INSTANCE_LOCK = new Object(); | |
public JFrame frame; | |
public PopupMenu defaultMenu; | |
public TrayIcon trayIcon; | |
public TaskTray(){ | |
createPopupMenu(); | |
try { | |
// LookAndFeelをWindowsに設定 | |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |
// 通常のアイコン | |
final Image img = new ImageIcon(ImageIO.read(new File("tray_icon.gif"))).getImage(); | |
final JFrame frame = new JFrame("タスクトレイにアイコンを表示・点滅"); | |
frame.setSize(300, 100); | |
frame.setIconImage(img); | |
// ウィンドウが最小化された時にタスクトレイに格納する | |
// ようにWindowListenerクラスを追加。 | |
frame.addWindowListener(new WindowAdapter() { | |
public void windowIconified(WindowEvent e) { | |
frame.setVisible(false); | |
} | |
}); | |
JPanel jPanel = new JPanel(); | |
jPanel.setLayout(new FlowLayout()); | |
jPanel.setSize(new Dimension(219, 70)); | |
// 点滅アイコン | |
final Image imgLight = new ImageIcon(ImageIO.read(new File("tray_icon_light.gif"))).getImage(); | |
JButton lightButton = new JButton("点滅"); | |
lightButton.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
onAndOffLight(imgLight, null); | |
displayMessage("メッセージ", "ここから出してくれ!!"); | |
} | |
}); | |
JButton cancelButton = new JButton("解除"); | |
cancelButton.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
replaceImageWithDelete(img); | |
} | |
}); | |
jPanel.add(lightButton, null); | |
jPanel.add(cancelButton, null); | |
frame.add(jPanel); | |
// 終了時処理。 | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
// フレームを画面中央に表示。 | |
frame.setLocationRelativeTo(null); | |
// タスクトレイを作成する。 | |
createTray(frame); | |
frame.setVisible(true); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* タスクトレイに表示するデフォルトのPopupMenuメニューを生成します。 以下のメニューがあります。 ・「タスクトレイから取り出す」 | |
* タスクトレイから対象のアプリケーションをデスクトップ上に 取り出します。 (ウィンドウをアクティブにし、手前に表示する。) | |
* ・終了対象のアプリケーションを終了させます。 | |
*/ | |
private void createPopupMenu() { | |
MenuItem getTrayItem = new MenuItem("タスクトレイから取り出す"); | |
getTrayItem.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
active(); | |
} | |
}); | |
MenuItem exitItem = new MenuItem("終了"); | |
exitItem.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
removeTrayIcon(); | |
frame.setVisible(false); | |
frame.dispose(); | |
System.exit(0); | |
} | |
}); | |
defaultMenu = new PopupMenu(); | |
defaultMenu.add(getTrayItem); | |
defaultMenu.add(exitItem); | |
} | |
/** | |
* トレイアイコンを作成し、システムトレイに追加する。 アイコン上で発生したイベントに対するリスナを targetFrame に設定します。 | |
* ここでのイベントは、トレイ・アイコンをダブルクリックされた時に 対象のアプリケーションを取り出します。 | |
* | |
* @param targetFrame | |
* 対象のアプリケーション | |
*/ | |
public void createTray(JFrame targetFrame) { | |
createTray(targetFrame, null, null); | |
} | |
/** | |
* トレイアイコンを作成し、システムトレイに追加する。 | |
* | |
* @param targetFrame | |
* 対象のアプリケーション | |
* @param image | |
* トレイアイコンに表示するイメージ画像。 nullの場合、targetFrame からgetIconImage()で取得できる | |
* イメージ画像を使用します。 | |
* @param menu | |
* タスクトレイに表示するPopupMenu。 nullの場合、デフォルトのPopupMenuを表示します。 | |
*/ | |
public void createTray(final JFrame targetFrame, Image image, PopupMenu menu) { | |
// システム・トレイがサポートされていなければ終了。 | |
if (!SystemTray.isSupported()) { | |
return; | |
} | |
frame = targetFrame; | |
if (image == null) { | |
image = targetFrame.getIconImage(); | |
} | |
if (menu == null) { | |
menu = defaultMenu; | |
} | |
trayIcon = new TrayIcon(image, targetFrame.getTitle(), menu); | |
trayIcon.setImageAutoSize(true); | |
trayIcon.addMouseListener(new MouseAdapter() { | |
public void mouseClicked(MouseEvent e) { | |
if (SwingUtilities.isLeftMouseButton(e)) { | |
active(); | |
} | |
} | |
}); | |
try { | |
TRAY.add(trayIcon); | |
} catch (AWTException e1) { | |
e1.printStackTrace(); | |
} | |
} | |
/** | |
* タスクトレイからアイコンを削除する。 アプリケーション終了時に呼び出す必要がある。 | |
*/ | |
public void removeTrayIcon() { | |
TRAY.remove(trayIcon); | |
} | |
/** | |
* タスクトレイのアイコンを点滅させる。 | |
* | |
* @param msg | |
* 必要であれば点滅されたアイコンがクリックされた時に 表示するメッセージをセットします。 | |
*/ | |
public void onAndOffLight(Image lightImg, String msg) { | |
replaceImage(lightImg); | |
ChangeLightImgListener listener = new ChangeLightImgListener(msg, this, frame, trayIcon); | |
trayIcon.addMouseListener(listener); | |
frame.addWindowListener(listener); | |
} | |
/** | |
* タスクトレイのアイコンを変更する。 | |
* | |
* @param image | |
* 現在のアイコンと違う場合のみ変更する。 | |
*/ | |
public void replaceImage(Image image) { | |
synchronized (INSTANCE_LOCK) { | |
if (!image.equals(trayIcon.getImage())) { | |
trayIcon.setImage(image); | |
} | |
} | |
} | |
/** | |
* タスクトレイのアイコンを変更する。 ※何故か点滅しているアイコンを戻す場合は、 | |
* {@link SystemTray#remove(TrayIcon)})で削除しないと 駄目なようである。 | |
* | |
* @param image | |
* 現在のアイコンと違う場合のみ変更する。 | |
*/ | |
public void replaceImageWithDelete(Image image) { | |
synchronized (INSTANCE_LOCK) { | |
if (!image.equals(trayIcon.getImage())) { | |
TRAY.remove(trayIcon); | |
trayIcon.setImage(image); | |
try { | |
TRAY.add(trayIcon); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
/** | |
* タスクトレイにポップアップ・メッセージを表示させます。 | |
* | |
* @param caption | |
* @param text | |
*/ | |
public void displayMessage(String caption, String text) { | |
if (caption != null || text != null) { | |
trayIcon.displayMessage(caption, text, TrayIcon.MessageType.INFO); | |
} | |
} | |
/** | |
* タスクトレイから対象のアプリケーションをデスクトップ上に 取り出します。 | |
*/ | |
private void active() { | |
// フレームの状態を通常に戻す。 | |
frame.setExtendedState(JFrame.NORMAL); | |
frame.setAlwaysOnTop(true); | |
frame.setVisible(true); | |
frame.setAlwaysOnTop(false); | |
} | |
public static void main(String[] args) { | |
new TaskTray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment