문제

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