Java setPressedIcon not working

巧了我就是萌 提交于 2019-12-11 23:19:46

问题


I have menu in my application, and I want to set menu item normal state icon, and pressed state icon. Normal state icon is added, but when I press menu item, normal state icon is not changed by pressed state icon. What is problem here:

        JMenu m=new JMenu(text);
        m.setBackground(getTheme().colors.menuColor());
        m.setOpaque(false);
        m.setIcon(core.getIcon(text, "normal"));
        m.setPressedIcon(core.getIcon("webmaps", "pressed"));

回答1:


This issue has been seen before. The inherited setPressedIcon does not change the background Icon on the the JMenu (or indeed JMenuItem). You could use a MenuListener on the component as a workaround:

m.addMenuListener(new MenuListener() {

    @Override
    public void menuSelected(MenuEvent e) {
        JMenu menu = (JMenu) e.getSource();
        menu.setIcon(core.getIcon("webmaps", "pressed"));
    }

    @Override
    public void menuDeselected(MenuEvent e) {
        JMenu menu = (JMenu) e.getSource();
        menu.setIcon(core.getIcon(text, "normal"));
    }

    @Override
    public void menuCanceled(MenuEvent e) {
        JMenu menu = (JMenu) e.getSource();
        menu.setIcon(core.getIcon(text, "normal"));
    }
});


来源:https://stackoverflow.com/questions/15034573/java-setpressedicon-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!