What do you mean by use the JMenuBar? I would suggest looking into OOP and encapsulation. There's a guy on YouTube that I think does an excellent job covering stuff like that as well as several Design Patterns, the channel name I believe is DerekBanas. He'll show up if you google it.
A quick answer to your question is you're going to want to pass something into the constructor of the JPanel class either that or put the main method in the JPanel class, here's a quick example of one way to do this...
JMenuItem is - add new record , in first tab when I choose this JMenuItem he opens me a frame, where are JLabel and JTextField. Then there is a second tab, I click on the same JMenuItem and it's summons new JFrame which have it's own JLabel and JTextField.
public class TestFrame extends JFrame {
private ActionManager actionManager
private JMenuBar mb;
private JMenu file;
private JMenuItem openDialog1;
private JMenuItem openDialog2;
public TestFrame() {
this.actionManager = new ActionManager();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPanel(panel);
setJMenuBar(createMenuBar());
pack();
}
private JMenuBar createMenuBar() {
mb = new JMenuBar();
file = new JMenu("File");
openDialog2 = new JMenuItem("Open Dialog 2");
openDialog1 = new JMenuItem("Open Dialog 1");
openDialog2.addActionListener(actionManager.openDialog2Action);
openDialog1.addActionListener(actionManager.openDialog1Action);
//here i would add conditional code that added the correct
//menus to the menubar and menuitems to the correct menus and call
//this method from a `ChangeListener` that listens for tab changes so
//it recreates a new menu with the correct components for the selected
//tab but i added them to the same menu to demonstrate using specific actions
//for each menu item, it doesn't matter which menu they're attached to their
//action wont be called unless that JMenuItem is clicked.
//this method can be used for any type of button as well, commonly with
//toolbars, so you can reuse actions like copy, paste, new, open, save, etc
file.add(openDialog1);
file.add(openDialog1);
mb.add(file);
return mb;
}
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
And the panel class...
public class ActionManager {
public ActionManager() {
}
Action openDialog1Action = new AbstractAction("Open Dialog 1") {
JOptionPane.showMessageDialog(null, "Dialog 1");
}
Action openDialog2Action = new AbstractAction("Open Dialog 2") {
JOptionPane.showMessageDialog(null, "Dialog 2");
}
}
All this code does is use public methods to access private fields from the class (encapsulation) and the TestFrame object is passed into the TestPanel constructor so we can use the getter methods inside TestFrame from the TestPanel class. I also added the ActionListener as part of the TestPanel class because that's another way to use the menubar components in the panel class. Can you tell me specifically what you're trying to accomplish, or possibly post a small portion of your current code? This is one of several ways to do what you asked but it may not be the preferred way depending on why you want access to them from the panel in the first place, the more specific your questions are the more help I can be. Good luck.