TITLE:TrayIconでJPopupMenuを使用する

Posted by aterai at 2011-01-31

TrayIconでJPopupMenuを使用する

TrayIconをクリックしてJPopupMenuを表示します。

  • &jar;
  • &zip;
TrayIconPopupMenu.png

サンプルコード

final SystemTray tray  = SystemTray.getSystemTray();
final Image image      = new ImageIcon(getClass().getResource("16x16.png")).getImage();
final TrayIcon icon    = new TrayIcon(image, "TRAY", null);
final JPopupMenu popup = new JPopupMenu();
final JDialog dummy    = new JDialog();
dummy.setUndecorated(true);
popup.addPopupMenuListener(new PopupMenuListener() {
  @Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}
  @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
    dummy.setVisible(false);
  }
  @Override public void popupMenuCanceled(PopupMenuEvent e) {
    dummy.setVisible(false);
  }
});
icon.addMouseListener(new MouseAdapter() {
  private void showJPopupMenu(MouseEvent e) {
    if(e.isPopupTrigger()) {
      Point p = adjustPopupLocation(popup, e.getX(), e.getY());
      dummy.setLocation(p);
      dummy.setVisible(true);
      popup.show(dummy, 0, 0);
    }
  }
  @Override public void mouseReleased(MouseEvent e) {
    showJPopupMenu(e);
  }
  @Override public void mousePressed(MouseEvent e) {
    showJPopupMenu(e);
  }
});

解説

Java 6 では、TrayIconにはjava.awt.PopupMenuしか使用できないので、setUndecorated(true)かつ、サイズが0x0のJDialogを適当な位置(TrayIconのクリックでJPopupMenuが開いたように見える場所)に配置し、これを親にしてjavax.swing.JPopupMenuを表示しています。


PopupMenuではなく、JPopupMenuが使用できるので、以下のようなことが可能になります。

  • JCheckBoxMenuItem、JRadioButtonMenuItemの使用
  • LookAndFeelの変更

このサンプルでは、JPopupMenu#adjustPopupLocationToFitScreen(...)メソッドを改変して、SystemTrayの位置によってJPopupMenuが画面外にはみ出さないように調整しています。

//Copied from JPopupMenu.java
private static Point adjustPopupLocation(JPopupMenu popup, int xposition, int yposition) {
  Point p = new Point(xposition, yposition);
  if (GraphicsEnvironment.isHeadless()) return p;

  Rectangle screenBounds;
  GraphicsConfiguration gc = null;
  // Try to find GraphicsConfiguration, that includes mouse pointer position
  for (GraphicsDevice gd: GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
    if (gd.getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
      GraphicsConfiguration dgc = gd.getDefaultConfiguration();
      if (dgc.getBounds().contains(p)) {
        gc = dgc;
        break;
      }
    }
  }

  // If not found and popup have invoker, ask invoker about his gc
  if (gc == null && popup.getInvoker() != null) {
    gc = popup.getInvoker().getGraphicsConfiguration();
  }

  if (gc != null) {
    // If we have GraphicsConfiguration use it to get
    // screen bounds
    screenBounds = gc.getBounds();
  } else {
    // If we don't have GraphicsConfiguration use primary screen
    screenBounds = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
  }

  Dimension size = popup.getPreferredSize();

  // Use long variables to prevent overflow
  long pw = (long) p.x + (long) size.width;
  long ph = (long) p.y + (long) size.height;

  if (pw > screenBounds.x + screenBounds.width)  p.x -= size.width;
  if (ph > screenBounds.y + screenBounds.height) p.y -= size.height;

  // Change is made to the desired (X,Y) values, when the
  // PopupMenu is too tall OR too wide for the screen
  if (p.x < screenBounds.x) p.x = screenBounds.x;
  if (p.y < screenBounds.y) p.y = screenBounds.y;

  return p;
}

参考リンク

コメント

  • example.jarとsrc.zipを上げ忘れていたのを修正orz。 -- aterai