Change the title text of JMenuItem upon click?

 ̄綄美尐妖づ 提交于 2019-12-11 12:23:59

问题


I am adding a JMenuItem (Show History) that will toggle the appearance of a JPanel upon click. But after doing so, I want to change the title of that menu item to state the opposite action (Hide History). Is there a way to change just the text for that menu item, or must I remove the old JMenuItem and add a new one?

JMenuItem history = new JMenuItem("Show History");
history.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
        //code here to show the history
        //history.changeText("Hide History") OR viewMenu.remove(history) and create/add new one
    }
});
viewMenu.add(history);

回答1:


So this is what you do:

history.setText("Hide History");

And make history final.



来源:https://stackoverflow.com/questions/20234460/change-the-title-text-of-jmenuitem-upon-click

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