-1

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1938460
  • 75
  • 1
  • 1
  • 9
  • Can you provide some example code to show how you are loading the file and setting the tab content? – MadProgrammer Dec 30 '12 at 20:23
  • I cant upload any code due to security risks, but I any information would be helpful. So far the only way I think of doing it is by adding the menubar in the Tab, however this is more complicated and does not look nice. I was wondering if there is a way to link the menubar with the current tab only. as the changelistener chooses where the tab is. Thanks – user1938460 Dec 30 '12 at 20:37
  • Without an example of your problem, it's going to be impossible to provide you with assistance... – MadProgrammer Dec 30 '12 at 20:38
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Dec 30 '12 at 23:07

1 Answers1

2

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045