JMenu submenu little arrow icon indicator

早过忘川 提交于 2019-12-02 09:56:17

Take a look at the Menu.arrowIcon UI property

(Thanks to AndrewThompson for the test code).

Doining this will effect ALL the menus created AFTER you apply the modifications.

So after you init Look and Feel and before you create any menus call UIManager.getLookAndFeelDefaults().put("Menu.arrowIcon", null);

I'd just like to say I think this is a terrible idea and would highly discourage you from doing it.

this arrow doesn't appear on top level JMenus only when they are submenus of other JMenu.

It seem (monotonously) consistent in its appearance using Metal here.

import javax.swing.*;

public class MenuArrows {

    MenuArrows() {
        JMenuBar mb = new JMenuBar();

        JMenu root1 = new JMenu("Root Menu 1"); 
        JMenu root2 = new JMenu("Root Menu 2"); 

        addSubMenus(root1, 5);
        addSubMenus(root2, 3);

        mb.add(root1);
        mb.add(root2);

        JOptionPane.showMessageDialog(null, mb);
    }

    public void addSubMenus(JMenu parent, int number) {
        for (int i=1; i<=number; i++) {
            JMenu menu = new JMenu("Sub Menu " + i);
            parent.add(menu);

            addSubMenus(menu, number-1);
            addMenuItems(menu, number);
        }
    }

    public void addMenuItems(JMenu parent, int number) {
        for(int i=1; i<=number; i++) {
            parent.add(new JMenuItem("Item " + i));
        }
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                new MenuArrows();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!