问题
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