1

I'm trying to create a frame with tabs and menubar, each tab will contain a different JTable, I wonder is it possible to add a constructor from different class to a tab? I mean:

public class Administrator() {
JFrame frame;
JTabbedPanel tabPan;
JPanel firstTab, secondTab;
JMenuBar menuBar;
JMenu menu;
JMenuItem mi1;
Administrator() {
  frame = new JFrame("Test");
  frame.setDefaultClopseOperation(JFrame.EXIT_ON_CLOSE);
  tabPan = new JTabbedPane();
  firstTab = new JPanel();
  secondTab = new JPanel();
  menuBar = new JMenuBar();
  menu = new JMenu("Menu");
  mi1 = new JMenuItem("1");
  menu.add(mi1);
  menuBar.add(menu);
  frame.addJMenuBar(menuBar);
  tabPan.addTab("First",firstTab);
  tabPan.addTab("Second",secondTab);
  frame.setVisible(true);
 }
 public static void main(String[] args) {
  new Administrator();
 }

and then the second(First) class:

public class First() {
  JTable firstTable = new JTable();
  //....
  First() {
  //...
  firstTab.add(firstTable);
  //...
}
}

EDIT

Ok I found a way to do so, Jtabbedpane using multiple classes , but now I have a different question, how can I use JMenuBar from main class? I extend JPanel, not the class, can I somehow use them in other class?

Community
  • 1
  • 1
usr999
  • 157
  • 2
  • 7
  • 20
  • I'm not sure what you're asking. You want to add a JMenuBar to a JPanel? If so you can add JMenuBars to custom components I cant remember off hand but I believe its an interface you must implement to allow it to contain a JMenu. If you look through the API starting with JFrame and follow the super classes you'll see it listed in the interfaces probably somewhere around the Window class or maybe its super class, it has an obvious name, if I think of it I'll let you know. If that isn't what you were asking please be more specific about what you want to do and I'll try to help. Good luck. – Kevin Bigler Apr 07 '13 at 12:28
  • Its in the Frame class, the interface is MenuContainer. I haven't messed with it but I'm pretty sure that will allow you to add a JMenuBar to a custom container. But I would consider trying to accomplish the same thing another way, a lot of times when things aren't often used its because there's a simpler way to do it, though that is not always the case. Just something to think about. – Kevin Bigler Apr 07 '13 at 12:33
  • Example: Two classes, one have a **JMenuBar**, second extends JPanel, can I somehow use use first class JMenuBar in second class? Now I can't because second class dosn't extend first class. – usr999 Apr 07 '13 at 12:36
  • I responded to that as an answer so I could format a short example program for you. In that program the menu is created in the frame class and references to it and its components are stored in the panel class. You should be able to use any part of the menubar in the panel class just like you would in the frame class. – Kevin Bigler Apr 08 '13 at 11:29

1 Answers1

1

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.

Kevin Bigler
  • 256
  • 1
  • 2
  • 9
  • Here's the problem I'm using the JTabbedPane and I want, when I use JMenuItem from JMenuBar, I want that the button made that I need in this particular tab. JMenuItem remaint the same in all tabs, but the call different methods. – usr999 Apr 08 '13 at 14:20
  • If I add `my_name.getMenuFromFrame();` to each tab, then the `ActionListener` opens all methods which ar added to that `JMenuItem` but I want to make that each tab will only summon those methods which I gave them for this particular tab. – usr999 Apr 08 '13 at 16:05
  • For example, first `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`. Different methods from different classes. Now, when I press the `JMenuItem` it summons all methods from all tabs. – usr999 Apr 08 '13 at 16:09
  • Oh you mean you want the frame's menus, menu items and actions to change and be custom to each tab. Okay so you're seeing the correct menus and correct menu items when you change tabs if I understand your second comment correctly so all you need to do is make sure only one action is called and its the correct action for the item, right? Instead of implementing `ActionListener` in the panel class use the `Action` and `AbstractAction` class and it can all be handled from the frame class or an action handler class, I'll edit the code in my previous answer to show you an example. – Kevin Bigler Apr 11 '13 at 02:55