Swing/SystemTray のバックアップソース(No.17)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/SystemTray へ行く。
- 1 (2007-05-08 (火) 20:23:38)
- 2 (2007-05-08 (火) 21:41:13)
- 3 (2007-05-09 (水) 13:54:25)
- 4 (2007-10-20 (土) 02:53:45)
- 5 (2009-04-13 (月) 14:23:41)
- 6 (2009-12-08 (火) 09:28:46)
- 7 (2010-03-05 (金) 21:05:36)
- 8 (2010-03-06 (土) 23:22:39)
- 9 (2010-03-08 (月) 17:28:43)
- 10 (2010-03-09 (火) 16:26:47)
- 11 (2010-09-30 (木) 18:16:34)
- 12 (2011-03-23 (水) 13:23:06)
- 13 (2013-02-09 (土) 23:46:19)
- 14 (2014-11-01 (土) 00:46:09)
- 15 (2014-12-25 (木) 16:24:23)
- 16 (2015-01-12 (月) 02:13:46)
- 17 (2015-04-01 (水) 20:10:37)
- 18 (2015-04-07 (火) 19:54:18)
- 19 (2016-09-06 (火) 12:53:43)
- 20 (2016-11-18 (金) 14:59:16)
- 21 (2017-11-02 (木) 15:34:40)
- 22 (2017-11-22 (水) 15:30:29)
- 23 (2019-06-25 (火) 16:04:37)
- 24 (2021-03-07 (日) 18:24:18)
- 25 (2022-08-20 (土) 22:15:25)
--- title: SystemTrayにアイコンを表示 tags: [SystemTray, Icon] author: aterai pubdate: 2007-03-05 description: JDK 6で追加された機能を使って、SystemTrayにアイコンを表示します。 --- * 概要 [#n4a8cf0f] `JDK 6`で追加された機能を使って、`SystemTray`にアイコンを表示します。 #download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTUJeisovI/AAAAAAAAAlk/zvAoP96Ntcs/s800/SystemTray.png) * サンプルコード [#ie1fffe5] #code(link){{ public MainPanel(final JFrame frame) { super(); if (!SystemTray.isSupported()) { frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); return; } final SystemTray tray = SystemTray.getSystemTray(); final Image image = new ImageIcon( getClass().getResource("16x16.png")).getImage(); final PopupMenu popup = new PopupMenu(); final TrayIcon icon = new TrayIcon(image, "TRAY", popup); MenuItem item1 = new MenuItem("OPEN"); item1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.setVisible(true); } }); MenuItem item2 = new MenuItem("EXIT"); item2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { tray.remove(icon); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.dispose(); //System.exit(0); } }); popup.add(item1); popup.add(item2); try { tray.add(icon); } catch (AWTException e) { e.printStackTrace(); } } }} * 解説 [#g1110d96] トレイアイコンでは、`JPopupMenu`ではなく、`PopupMenu`や`MenuItem`を使用します。 上記のサンプルでは、フレームがアイコン化(最小化)されたときにタスクバーの表示を消して、システムトレイにアイコンだけ表示したいので、初期状態を`frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);`にしておき、アイコン化された場合でも、`frame.dispose();`するようにしています。 実際に`VM`を終了する場合は、表示可能なウィンドウをすべて破棄して(`frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);`)、システムトレイからも`tray.remove(icon);`してアイコンを取り除けばいいようです。 * 参考リンク [#n62f3502] - [[TrayIconのアニメーション>Swing/AnimatedTrayIcon]] - [[TrayIconのダブルクリック>Swing/ClickTrayIcon]] * コメント [#na7a06ee] #comment - `Ubuntu`だと通知スペースに表示されるのですが、背景色や位置がどうもうまくいっていないようです。 -- &user(aterai); &new{2007-05-08 (火) 20:23:38}; -- 位置は以下みたいにすれば適当に補正できそうですが、背景色はどうしたらいいんだろう? `g.setColor(UIManager.getColor("Panel.background"));g.fillRect(0,0,d.width,d.height);`とかするのは酷いか…。 -- &user(aterai); &new{2007-05-08 (火) 21:11:09}; #code{{ SystemTray tray = SystemTray.getSystemTray(); Dimension d = tray.getTrayIconSize(); //Image image = new ImageIcon(getClass().getResource("16x16.png")).getImage(); BufferedImage image = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB); ImageIcon i = new ImageIcon(getClass().getResource("16x16.png")); Graphics g = image.createGraphics(); i.paintIcon(null, g, (d.width - i.getIconWidth()) / 2, (d.height - i.getIconWidth()) / 2); g.dispose(); PopupMenu popup = new PopupMenu(); TrayIcon icon = new TrayIcon(image, "TRAY", popup); }} -- `g.setBackground(new Color(0,0,0,0));g.clearRect(0,0,d.width,d.height);`とかしても変化無し。 -- &user(aterai); &new{2007-05-09 (水) 13:54:25}; - メモ: [http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6707273 TrayIcon does not support 8-bit alpha channel in Windows XP] -- &user(aterai); &new{2009-12-08 (火) 09:28:46}; - タスクバーのアイコンがなくなりシステムトレイのアイコンだけになりませんか? -- &new{2010-03-05 (金) 21:05:36}; -- `Windows`でしか試していませんが、`JFrame`の代わりに、`JDialog`を使えばタスクバーには何も表示されないと思います(質問の意味を取り違えてなければいいのですが……)。 -- &user(aterai); &new{2010-03-06 (土) 23:22:39}; - 消したいのはタイトルバーではなくてシステムトレイの隣のタスクバーアイコンです。文字で伝えるのは難しいですね。 -- &new{2010-03-08 (月) 17:28:43}; - スクリーンショットで`JST`システムトレイとタスクバーにあるやつです -- &new{2010-03-08 (月) 17:30:21}; -- `JFrame`の代わりに、`JWindow`を使うのはどうでしょうか。 -- &user(aterai); &new{2010-03-09 (火) 16:26:47}; -- もしくは、[[JFrameのアイコンを非表示>Swing/DisableDefaultIcon]]のように透明なアイコンを設定する(クリックすると反応してしまいますが…)とか。 -- [[aterai]] -- `4`年以上前で、いまさら過ぎるのですが、やっと理解しました。自分の回答は的外れすぎですね。アイコン化したときにタスクバーボタンを非表示にするには、`Window#dispose()`を実行するとよさそうです。このサンプルを修正して非表示にするよう変更しました。 -- &user(aterai); &new{2014-12-25 (火) 16:35:38}; -- [[TrayIconの使用中にJFrameを最小化したとき、タスクバーボタンを非表示にする>Swing/HideTaskbarButton]]に移動 #comment