Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JFrames and JMenuBars

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JFrames and JMenuBars

    Hi all,

    I have a question I'd like to ask regarding JFrames and JMenuBars. My question is I have a main JFrame window which extends JFrame, and in this JFrame I have created a JMenuBar. I have created other JFrames which open up after JButtons have been clicked on and found that in each JFrame file, in order to show the same JMenuBar that is in the first JFrame window, I have to create the same methods that I created in the first JFrame in order to display the JMenuBar. To me this feels like unnecessary coding of the same thing when I could just have one that is common to all my JFrames but how do i do this?

    I have tried several things in order to save myself having to code the same methods in each JFrame, but have not come up with a successful solution, I can create the JMenuBar in the first JFrame, but it will not show in the next JFrame.

    public class FirstWindow extends JFrame implements ActionListener
    {
    	protected  JMenuBar myMenuBar = new JMenuBar();	
    	protected  JMenu myMenuFile = new JMenu("File");		
    	protected  JMenuItem myMenuItemExit = new JMenuItem("Exit");
     
    	private JButton btn;
    	protected JPanel pnl;
     
    	public FirstWindow()
    	{
    		setTitle("First Window");
    		setSize(700, 430);
    		setJMenuBar(myMenuBar);
    		this.getContentPane().setBackground( Color.yellow);
    		createMenu();
    		setVisible(true);
    		setResizable(false);
     
    		pnl = new JPanel();
    		btn = new JButton("Switch to Next Window");
    		btn.addActionListener(this);
    		pnl.add(btn, BorderLayout.CENTER);
    		add( pnl, BorderLayout.CENTER );
     
    	}
     
    	protected  void buildMenu()
    	{
    		myMenuFile.add(myMenuItemExit);
    		myMenuItemExit.addActionListener( this );
    		myMenuBar.add(myMenuFile);	
    	}
     
    	public void actionPerformed( ActionEvent e )
    	{
                    if ( e.getSource() == btn )
                    {
                            string msg = "Add An exit here";
                            JOptionPane.showMessageDialog( this, msg, "Exit Message", JOptionPane.OK_OPTION );
            }

    public class TestExtend extends JFrame
    {	
    	public TestExtend()
    	{
    		setTitle("Test Frame 1");
    		setSize(700, 430);
    	}
            public TestExtend(Mgw w)
    	{
    		w.buildMenu();
    	}
    }

    public class MainP
    {
    	public static void main(String[] args)
    	{
    		FirstWindow firstwin = new FirstWindow();
    		firstwin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    	}
    }

    From my code above, the new window will show, but without the JMenuBar which I created in the first one.

    Just to clarify, I would like to know if there is a way to save myself coding the same JMenuBar in each frame I create by sharing the one from the first frame?

    Thanks if you can help


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JFrames and JMenuBars

    One possibility is to create a class independent of your JFrame that extends or has a function which returns a JMenuBar to use. When you use this class/method, pass in the ActionListener

    public class MyMenuBar extends JMenuBar{
        public MyMenuBar(ActionListener listener){
            JMenu fileMenu = new JMenu("File");
            JMenuItem open = new JMenu("Open");
            open.addActionListener(listener);
            fileMenu.add(open);
            add(fileMenu);
        }
    }
    Now you can use instance of this class for each JFrame. It gets a bit more difficult if you have multiple ActionListeners (which in the case of redundant tasks you should), but one way to get around this is to use a single one to pass to the above and have it delegate its tasks to each independent Listener responsible for the particular tasks.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: JFrames and JMenuBars

    Well, if you made the menu bar public in the first one, then perhaps you could call it in the others.

    For example,

    public class BlablaBlaa extends JFrame
    {

    public JMenuBar mBar;


    and later

    when adding all the menus you need

    in your other classes
    JMenuBar mBar2 = new JMenuBar();
    int x = FirstClass.mBar.getMenuCount();

    for (int v=0; v < x; v++)
    {
    mBar2.add(FirstClass.mbar.getMenu(v));
    }

    However, you'll have to add all the JMenuItems for each menu as well.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JFrames and JMenuBars

    Quote Originally Posted by javapenguin View Post
    Well, if you made the menu bar public in the first one, then perhaps you could call it in the others.
    ....
    However, you'll have to add all the JMenuItems for each menu as well.
    JMenuBar, JMenu, and JMenuItem all extend Component, which can only have a single parent. Adding them to another frame means they are removed from the previous. The results of this can get ugly real quick, especially when there are much better alternatives - such as a design for reusable components by creating a new class that is loosely coupled to the clients that use that class.
    Last edited by copeg; November 4th, 2010 at 05:17 PM.

Similar Threads

  1. JFrames not coming up?
    By scooty199 in forum AWT / Java Swing
    Replies: 13
    Last Post: October 30th, 2010, 02:54 AM
  2. Multiple JFrames
    By Scottj996 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 15th, 2010, 05:24 AM