JMenu work on single tab

2019-09-20 03:07发布

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条回答
冷血范
2楼-- · 2019-09-20 03:50

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.

查看更多
登录 后发表回答