javaswingbackgroundborderjpopupmenu

JPopupMenu remove background and border


JPopupMenu

I need to remove the background and Border in JPopupMenu. JPopupMenu should be completely transparent. Overriding the paintComponent does not give a positive result. I also tried to find the solution in BasicMenuItemUI/PopupMenuUI and MenuItemUI/BasicPopupMenuUI but found that the rendering background and border is not in them.

public class CustomMenuItemUI extends BasicMenuItemUI {
    protected MouseInputListener mouseInputListener;
    protected MenuDragMouseListener menuDragMouseListener;
    public static ComponentUI createUI(JComponent c) {
        return new CustomMenuItemUI();
    }

    private static float alpha = 0.0f;
    private static float selectionAlpha = 0.0f;

    public static float getAlpha() {
        return alpha;
    }

    public static void setAlpha(float _alpha) {
        alpha = _alpha;
    }

    @Override
    public void installUI(JComponent c) {
        super.installUI(c);
        menuItem.setOpaque(false);
    }

    @Override
    public void paint(Graphics g, JComponent comp) {
        Graphics2D gx = (Graphics2D) g.create();
        gx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        super.paint(gx, comp);
        gx.dispose();
    }

    @Override
    protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) {
        ButtonModel model = menuItem.getModel();
        Color oldColor = Color.red;
        int menuWidth = menuItem.getWidth();
        int menuHeight = menuItem.getHeight();
        if (model.isArmed() || (menuItem instanceof JMenu && model.isSelected())) {
            Graphics2D g2 = (Graphics2D) g.create(); 
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, selectionAlpha));
            g2.setColor(Color.red);
            g2.fillRect(0, 0, menuWidth, menuHeight);
        } else {
            g.setColor(Color.red);
            g.fillRect(0, 0, menuWidth, menuHeight);
        }
        g.setColor(oldColor);
    }

    @Override
    protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect, String text) {
        ButtonModel model = menuItem.getModel();
        if (model.isArmed() || model.isSelected() || !model.isSelected()) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
            super.paintText(g2, menuItem, textRect, text);
            g2.dispose();
        } else {
            super.paintText(g, menuItem, textRect, text);
        }
    }
    public static void setSelectionAlpha(float alpha) {
        selectionAlpha  = alpha;
    }
}

    String poppUI = CustomPopupMenuUI.class.getName(); 
UIManager.put("PopupMenuUI", poppUI);

Solution

  • You can change the PopupMenuUI delegate's properties, although there's no guarantee that your chosen Look & Feel will honor the changes. the change should be early in the program, before any GUI component is instantiated.

    UIManager.put("PopupMenu.background", new Color(0));
    UIManager.put("PopupMenu.border", BorderFactory.createEmptyBorder());
    

    Addendum: As @MadProgrammer commented, the platform's peer component may be opaque and outside your control. Your code (MenuTest, CustomPopupMenuUI, CustomMenuItemUI) has the appearance shown below on com.apple.laf.AquaLookAndFeel. Absent a better solution, you might look at implementing your own translucent window as shown here.

    image