TITLE:JPopupMenuを半透明にする

Posted by at 2012-02-27

JPopupMenuを半透明にする

JPopupMenuを半透明にします。

  • &jnlp;
  • &jar;
  • &zip;
TranslucentPopupMenu.png

サンプルコード

class TranslucentPopupMenu extends JPopupMenu{
  private static final Color ALPHA_ZERO = new Color(0, true);
  private static final Color POPUP_BACK = new Color(250,250,250,200);
  private static final Color POPUP_LEFT = new Color(230,230,230,200);
  private static final int LEFT_WIDTH = 24;
  @Override public boolean isOpaque() {
    return false;
  }
  @Override public void updateUI() {
    super.updateUI();
    boolean isNimbus = UIManager.getBorder("PopupMenu.border")==null;
    if(isNimbus) {
      setBorder(new BorderUIResource(BorderFactory.createLineBorder(Color.GRAY)));
    }
  }
  @Override public JMenuItem add(JMenuItem menuItem) {
    menuItem.setOpaque(false);
    return super.add(menuItem);
  }
  @Override public void show(Component c, int x, int y) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        Window p = SwingUtilities.getWindowAncestor(TranslucentPopupMenu.this);
        if(p!=null && p instanceof JWindow) {
          System.out.println("Heavy weight");
          JWindow w = (JWindow)p;
          if(System.getProperty("java.version").startsWith("1.6.0")) {
            w.dispose();
            if(com.sun.awt.AWTUtilities.isWindowOpaque(w)) {
              com.sun.awt.AWTUtilities.setWindowOpaque(w, false);
            }
            w.setVisible(true);
          }else{
            w.setBackground(ALPHA_ZERO);
          }
        }else{
          System.out.println("Light weight");
        }
      }
    });
    super.show(c, x, y);
  }
  @Override protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D)g.create();
    g2.setPaint(POPUP_LEFT);
    g2.fillRect(0,0,LEFT_WIDTH,getHeight());
    g2.setPaint(POPUP_BACK);
    g2.fillRect(LEFT_WIDTH,0,getWidth(),getHeight());
    g2.dispose();
    //super.paintComponent(g);
  }
}
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、JPopupMenuは、isOpaque()メソッドをオーバーライド、JMenuItemはsetOpaque(false) として、それぞれ透明に設定し、JPopupMenu#paintComponent(...)で、半透明の背景を描画しています。

JPopupMenuが親フレームの外にはみ出す場合は、HeavyweightのJWindowを使ってJPopupMenuが表示されるので、JWindow#setBackground(new Color(0, true))で(JDK 1.6.0_10では、com.sun.awt.AWTUtilities.setWindowOpaque(w, false))JWindow自体も透明にしています。

参考リンク

  • Translucent and Shaped Swing Windows | Java.net
    • メモ: PopupFactoryを継承するTranslucentPopupFactoryを作成して、PopupFactory.setSharedInstance(new TranslucentPopupFactory())と設定する方法
    • 以下、JMenuから開いたJPopupMenuも半透明にできないか、TranslucentPopupFactoryを使う方法をテスト中。
      • PopupFactory.setSharedInstance(new TranslucentPopupFactory());としないと、ルートJPopupMenuがLight weightで、JMenuのJPopupMenuがHeavy weightのときに、JMenuのJPopupMenuが半透明にならない理由が分からない…。
//package example;
//-*- mode:java; encoding:utf8n; coding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;

public class TranslucentMenuTest {
  private final JComponent tree = new JTree();
  public JComponent makeUI() {
    tree.setComponentPopupMenu(makePopupMenu());
    JPanel p = new JPanel(new BorderLayout());
    p.add(new JScrollPane(tree));
    return p;
  }
//   @Override public void updateUI() {
//     super.updateUI();
//     if(tree!=null && System.getProperty("java.version").startsWith("1.6.0")) {
//       System.out.println("???: 1.6.0_xx bug??? remake JPopupMenu");
//       tree.setComponentPopupMenu(makePopupMenu());
//     }
//   }
  private static JPopupMenu makePopupMenu() {
    JPopupMenu popup = new TranslucentPopupMenu();
//*
    JMenu menu = new JMenu("Test") {
      //http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4688783
      private JPopupMenu popupMenu;
      private void ensurePopupMenuCreated() {
        if (popupMenu == null) {
          final JMenu thisMenu = this;
          this.popupMenu = new TranslucentPopupMenu();
          popupMenu.setOpaque(false);
          popupMenu.setInvoker(this);
          popupListener = createWinListener(popupMenu);
        }
      }
      @Override public JPopupMenu getPopupMenu() {
        ensurePopupMenuCreated();
        return popupMenu;
      }
      @Override public JMenuItem add(JMenuItem menuItem) {
        ensurePopupMenuCreated();
        menuItem.setOpaque(false);
        return popupMenu.add(menuItem);
      }
      @Override public Component add(Component c) {
        ensurePopupMenuCreated();
        if(c instanceof JComponent) {
          ((JComponent)c).setOpaque(false);
        }
        popupMenu.add(c);
        return c;
      }
      @Override public void addSeparator() {
        ensurePopupMenuCreated();
        popupMenu.addSeparator();
      }
      @Override public void insert(String s, int pos) {
        if (pos < 0) {
          throw new IllegalArgumentException("index less than zero.");
        }
        ensurePopupMenuCreated();
        popupMenu.insert(new JMenuItem(s), pos);
      }
      @Override public JMenuItem insert(JMenuItem mi, int pos) {
        if (pos < 0) {
          throw new IllegalArgumentException("index less than zero.");
        }
        ensurePopupMenuCreated();
        popupMenu.insert(mi, pos);
        return mi;
      }
      @Override public void insertSeparator(int index) {
        if (index < 0) {
          throw new IllegalArgumentException("index less than zero.");
        }
        ensurePopupMenuCreated();
        popupMenu.insert( new JPopupMenu.Separator(), index );
      }
      @Override public boolean isPopupMenuVisible() {
        ensurePopupMenuCreated();
        return popupMenu.isVisible();
      }
    };
/*/
    JMenu menu = new JMenu("Test") {
      @Override public JMenuItem add(JMenuItem menuItem) {
        menuItem.setOpaque(false);
        return super.add(menuItem);
      }
      @Override public Component add(Component c) {
        if(c instanceof JComponent) {
          ((JComponent)c).setOpaque(false);
        }
        return super.add(c);
      }
    };
//*/
    menu.setOpaque(false);
    menu.getPopupMenu().setOpaque(false);

    menu.add(new JMenuItem("Undo"));
    menu.add(new JMenuItem("Redo"));
    popup.add(menu);
    popup.addSeparator();
    popup.add(new JMenuItem("Cut"));
    popup.add(new JMenuItem("Copy"));
    popup.add(new JMenuItem("Paste"));
    popup.add(new JMenuItem("Delete"));
    return popup;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
//     try{
//       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//     }catch(Exception e) {
//       e.printStackTrace();
//     }

    PopupFactory.setSharedInstance(new TranslucentPopupFactory());

    JFrame frame = new JFrame("@title@");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new TranslucentMenuTest().makeUI());
    frame.setSize(320, 240);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}
//http://terai.xrea.jp/Swing/TranslucentPopupMenu.html
class TranslucentPopupMenu extends JPopupMenu {
  private static final Color ALPHA_ZERO = new Color(0, true);
  private static final Color POPUP_BACK = new Color(250,250,250,200);
  private static final Color POPUP_LEFT = new Color(230,230,230,200);
  private static final int LEFT_WIDTH = 24;
  @Override public boolean isOpaque() {
    return false;
  }
//   @Override public void updateUI() {
//     super.updateUI();
//     //Test:
//     boolean isNimbus = UIManager.getBorder("PopupMenu.border")==null;
//     if(isNimbus) {
//       //XXX: NimbusLnF not work???
//       setBorder(new BorderUIResource(BorderFactory.createLineBorder(Color.GRAY)));
//     }
//   }
  @Override public Component add(Component c) {
    if(c instanceof JComponent) {
      ((JComponent)c).setOpaque(false);
    }
    return c;
  }
  @Override public JMenuItem add(JMenuItem menuItem) {
    menuItem.setOpaque(false);
    return super.add(menuItem);
  }
  @Override public void show(Component c, int x, int y) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        Window p = SwingUtilities.getWindowAncestor(TranslucentPopupMenu.this);
        if(p!=null && p instanceof JWindow) {
          System.out.println("Heavy weight");
          ((JWindow)p).setBackground(ALPHA_ZERO);
        } else {
          System.out.println("Light weight");
        }
      }
    });
        Window p = SwingUtilities.getWindowAncestor(TranslucentPopupMenu.this);
        if(p!=null && p instanceof JWindow) {
          System.out.println("Heavy weight");
          ((JWindow)p).setBackground(ALPHA_ZERO);
        } else {
          System.out.println("Light weight");
        }
    super.show(c, x, y);
  }
  @Override protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D)g.create();
    g2.setPaint(POPUP_LEFT);
    g2.fillRect(0,0,LEFT_WIDTH,getHeight());
    g2.setPaint(POPUP_BACK);
    g2.fillRect(LEFT_WIDTH,0,getWidth(),getHeight());
    g2.dispose();
  }
}

//
//<a href="http://today.java.net/pub/a/today/2008/03/18/translucent-and-shaped-swing-windows.html">Translucent and Shaped Swing Windows | Java.net</a>
class TranslucentPopupFactory extends PopupFactory {
  @Override public Popup getPopup(Component owner, Component contents, int x, int y) throws IllegalArgumentException {
    return new TranslucentPopup(owner, contents, x, y);
  }
}

class TranslucentPopup extends Popup {
  private JWindow popupWindow;

  public TranslucentPopup(Component owner, Component contents, int ownerX, int ownerY) {
    // create a new heavyweight window
    this.popupWindow = new JWindow();
    // mark the popup with partial opacity
    //com.sun.awt.AWTUtilities.setWindowOpacity(popupWindow, (contents instanceof JToolTip) ? 0.8f : 0.95f);
    //popupWindow.setOpacity(.5f);
    // determine the popup location
    popupWindow.setLocation(ownerX, ownerY);
    // add the contents to the popup
    popupWindow.getContentPane().add(contents, BorderLayout.CENTER);
    contents.invalidate();
    JComponent parent = (JComponent) contents.getParent();
    // set the shadow border
    //parent.setBorder(new ShadowPopupBorder());
  }

  @Override
  public void show() {
    this.popupWindow.setVisible(true);
    this.popupWindow.pack();
    // mark the window as non-opaque, so that the
    // shadow border pixels take on the per-pixel
    // translucency
    //com.sun.awt.AWTUtilities.setWindowOpaque(this.popupWindow, false);
      popupWindow.setBackground(new Color(0,0,0,0));
  }

  @Override
  public void hide() {
    this.popupWindow.setVisible(false);
    this.popupWindow.removeAll();
    this.popupWindow.dispose();
  }
}

コメント