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);
有帮助吗?

解决方案

So this is what you do:

history.setText("Hide History");

And make history final.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top