SWT Tips and Samples 基礎編 > TaskTray

 

TaskTray

SWT3.0からタスクトレイの機能が追加されています。TaskTrayの使用方法は以下のようになります。

//タスクトレイを取得
TaskTray tray = display.getTaskTray();

//タスクトレイにトレイアイテムを作成
TrayItem trayItem = new TrayItem(tray, SWT.NONE);
trayItem.setToolTipText("TaskTray Test");
trayItem.setImage(new Image(display, "icon.gif"));

また、作成したTrayItemにリスナーを登録するとこで、タスクトレイアイコンの操作に対する処理を行うことができます。

trayItem.addListener(SWT.Show, new Listener() {
   public void handleEvent(Event event) {
      ...
   }
});
trayItem.addListener(SWT.Hide, new Listener() {
   public void handleEvent(Event event) {
      ...
   }
});
trayItem.addListener(SWT.Selection, new Listener() {
   public void handleEvent(Event event) {
       //タスクトレイのアイコンが左クリックされると呼び出される。
   }
});
trayItem.addListener(SWT.DefaultSelection, new Listener() {
    public void handleEvent(Event event) {
       //タスクトレイのアイコンがダブルクリックされると呼び出される。
    }
});

タスクトレイにメニューを追加することもできます。以下に示すようにMenuDetectイベントリスナーを登録することで、タスクトレイアイコンが右クリックされたとき、メニューが表示されます。

//メニューを作成
final Menu menu = new Menu(shell, SWT.POP_UP);
MenuItem item = new MenuItem(menu, SWT.PUSH);
...

//タスクトレイのアイコンが右クリックされたとき、メニューを表示するように設定
trayItem.addListener(SWT.MenuDetect, new Listener() {
   public void handleEvent(Event event) {
      menu.setVisible(true);
   }
});

サンプルプログラム

常駐型アプリケーションのサンプルです。サンプルを実行するには、javaコマンドを起動させるディレクトリにicon.gifを配置してください。サンプルを起動すると、ウィンドウが表示され、タスクトレイにアイコンが作成されます。

  

アプリケーションウィンドウのクローズボックスを閉じても、アプリケーションは終了せず、ウィンドウが見えない状態になるだけです。タスクトレイのアイコンをダブルクリックするとアプリケーションウィンドウが再度表示されます。

アプリケーションを終了させるには、タスクトレイアイコンのメニューよりExitを選びます。

また、タスクトレイのメニューには、Push型、Check型、Radio型メニューの作成例も示しています。

ソースコード (TaskTrayTest.java)

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Image;

public class TaskTrayTest {

   boolean isExiting = false;

   public static void main(String[] args) {
      new TaskTrayTest();
   }
   public TaskTrayTest() {
      Display display = new Display();
      final Shell shell = new Shell(display);
      shell.setText("TaskTrayTest");

      Tray tray = display.getSystemTray();
      TrayItem trayItem = new TrayItem(tray, SWT.NONE);
      trayItem.setToolTipText("TaskTray Test");
      trayItem.setImage(new Image(display, "icon.gif"));

      //イベントリスナーの登録
      trayItem.addListener(SWT.Show, new Listener() {
         public void handleEvent(Event event) {
            System.out.println("Show");
         }
      });
      trayItem.addListener(SWT.Hide, new Listener() {
         public void handleEvent(Event event) {
            System.out.println("Hide");
         }
      });
      trayItem.addListener(SWT.Selection, new Listener() {
         public void handleEvent(Event event) {
            System.out.println("Selection: 左クリックされました");
         }
      });
      trayItem.addListener(SWT.DefaultSelection, new Listener() {
         public void handleEvent(Event event) {
            System.out.println("DefaultSelection: ダブルクリックされました");
            shell.setVisible(true);
         }
      });

      final Menu menu = new Menu(shell, SWT.POP_UP);

      MenuItem item1 = new MenuItem(menu, SWT.PUSH);
      item1.setText("Push");

      MenuItem item3 = new MenuItem(menu, SWT.CASCADE);
      item3.setText("Cascade");

      Menu submenu = new Menu(item3);
      item3.setMenu(submenu);

      MenuItem item3_1 = new MenuItem(submenu, SWT.PUSH);
      item3_1.setText("SubItem Push");

      MenuItem item4 = new MenuItem(menu, SWT.CHECK);
      item4.setText("Check");

      new MenuItem(menu, SWT.SEPARATOR);

      MenuItem item6 = new MenuItem(menu, SWT.RADIO);
      item6.setText("Radio1");

      MenuItem item7 = new MenuItem(menu, SWT.RADIO);
      item7.setText("Radio2");
      
      new MenuItem(menu, SWT.SEPARATOR);
      
      MenuItem menuExit = new MenuItem(menu, SWT.PUSH);
      menuExit.setText("Exit");
      menuExit.addSelectionListener(new SelectionAdapter() {
          public void widgetSelected(SelectionEvent e) {
             //タスクトレイアイコンからExitが選択されたときアプリを終了
             isExiting = true;
             shell.close();
          }
      });

      //タスクトレイのアイコンが右クリックされたとき、メニューを表示するように設定
      trayItem.addListener(SWT.MenuDetect, new Listener() {
         public void handleEvent(Event event) {
            menu.setVisible(true);
         }
      });

      shell.addShellListener(new ShellAdapter() {
         public void shellClosed(ShellEvent e) {
            //タスクトレイアイコンのExitメニューが選択されたときのみアプリを終了
            if (!isExiting) {
               shell.setVisible(false);
               e.doit = false;
               return;
            }
         }
      });

      shell.setSize(200,100);
      shell.open();
      shell.setVisible(true);

      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }
}


最新更新日: 2005年5月8日
 
関連リンク
Eclipse API ドキュメント

- PR -

★☆当選☆★・・・すれば?現金100万円当ります!!

★ フルーツメール【毎日懸賞】好評実施中!デジカメや金券が当たる! ★ 毎日懸賞

オフィス用品・オフィス家具 価 格 交 渉 可! 
◎ 目指せ★業界最安値 ★ ◎ オフィネット・ドットコム株式会社

マイクロソフト お得な見積! まとめての購入ならオトクな方法で。ライセンスだから管理も簡単。


Copyright(C) 2003,2004 Jasmin Project. All Right Reserved.
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送