2

My main class displays a JMenuBar. This JMenuBar is managed from "calculator.ui.MenuBar."

public JMenuBar createMenuBar() 
{
    JMenuBar menuBar = new JMenuBar();
    new calculator.ui.MenuBar(menuBar);
    return menuBar;
}

MenuBar creates my "File" JMenu and "Insert" JMenu.

public MenuBar(JMenuBar menuBar)
{
    new FileMenu(menuBar);
    new InsertMenu(menuBar);
}

FileMenu contains all the options for "File." In the File class, there is a JMenuItem called "New Calculator." Now, when you click "New Calculator," I want the JPanel in my main class to create an instance of Calculator in my main class.

newFileSubMenu = new JMenu("New...");
calculatorFileSubMenu = new JMenuItem("New Calculator");

calculatorFileSubMenu.getAccessibleContext().setAccessibleDescription(
                "New Calculator");

newFileSubMenu.add(calculatorFileSubMenu);

ActionListener newCalculatorListener = new ActionListener() 
    {
    public void actionPerformed(ActionEvent event) 
    {
       newCalculator();
    }
};
calculatorFileSubMenu.addActionListener(newCalculatorListener);

This is the code for my main class JPanel:

public Container createContentPane() {
    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setOpaque(true);

    JTabbedPane tabbedPane = new JTabbedPane();

    return contentPane;
}

My questions are related to the design of my program. For each instance of Calculator, I want to:

  1. Create a JPanel within the main JPanel that contains my Calculator (what stumps me here is how do I - from my FileMenu class - create a JPanel that's in my main class?).
  2. Make sure that the Calculator object refreshes.

Note: I also want my JPanels to be in TabbedPanes (if that changes anything; if it doesn't, then I can figure that part out once I know the answer for the first question.)

Thanks for your help, I hope I've been clear enough in what I want to do.

helsont
  • 4,347
  • 4
  • 23
  • 28

1 Answers1

2

In your menu item's Action, you can use setSelectedIndex() on your JTabbedPane to select the pane holding an existing calculator instance. You can use setComponentAt() to replace the content of any tab with an instance of your calculator.

There's a related example here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    I followed your path and changed my constructors for my Menus to include my main JPanel. Then, when I hit the "New Calculator" button, I used JTabbedPane.addTab to create the tab. Thank you! – helsont Jun 16 '12 at 20:43