JMenu work on single tab

こ雲淡風輕ζ 提交于 2019-12-25 17:47:06

问题


I have a JMenuBar and I have a JTabbedPane, when I create multiple tabs the JMenuBar does it on all the tabs, e.g. I open a file it opens the same file in each tab.

However I only want it to open a file on the current tab. I have added a ChangeListener so It know what tab its in however it does not seem help even though it works.

tabbedPane.addChangeListener(new ChangeListener() {
    public void stateChanged(ChangeEvent e) {
        System.out.println("Tab=" + tabbedPane.getSelectedIndex());
    }
});
getContentPane().add(tabbedPane, "Center");
pack();

I want to be able to have multiple tabs open while the menu bar only corresponds to current tab.


回答1:


Let the object providing the content of each tab export it's own specific instances of Action. When the user changes tabs, use setAction() to set the desired action of each available JMenuItem in the global menu. There's a related example here.



来源:https://stackoverflow.com/questions/14094169/jmenu-work-on-single-tab

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