JMenuBar and JPanel

Discussion in 'Java' started by Ajay, Sep 23, 2005.

  1. Ajay

    Ajay Guest

    It seems JMenuBar can be added only to JInternalFrame and thus
    appearing only at the North of the frame..

    There is also some sample program on the sun site showing how you can
    add on the west side.

    I was wondering if anyone figured out a way to add it to a JPanel there
    by you could add it to any part of the interface..

    Thanks,
    Ajay.
    Ajay, Sep 23, 2005
    #1
    1. Advertising

  2. Ajay

    zero Guest

    "Ajay" <> wrote in
    news::

    > It seems JMenuBar can be added only to JInternalFrame and thus
    > appearing only at the North of the frame..
    >
    > There is also some sample program on the sun site showing how you can
    > add on the west side.
    >
    > I was wondering if anyone figured out a way to add it to a JPanel
    > there by you could add it to any part of the interface..
    >
    > Thanks,
    > Ajay.
    >



    A JMenuBar is a Component, so you can add it to any Container -
    including JPanels. The following example looks way ugly, but it should
    prove that it is in fact possible to put a JMenuBar just about anywhere
    you want. It has a menu bar at the top level on the south of the JFrame
    contentpane, and on a separate JPanel. To make it extra clear, this
    second menu bar is in the center of the JPanel (which in turn is in the
    center of the contentpane). To show where the JPanel is I gave it a red
    border.

    public class TestJMenuBar extends javax.swing.JFrame
    {
    public TestJMenuBar()
    {
    super("TestJMenuBar");

    javax.swing.JMenuBar sbar = new javax.swing.JMenuBar();
    javax.swing.JMenu smenu = new javax.swing.JMenu("South menu");
    javax.swing.JMenuItem item = new javax.swing.JMenuItem("item");
    smenu.add(item);
    sbar.add(smenu);

    javax.swing.JMenuBar cbar = new javax.swing.JMenuBar();
    javax.swing.JMenu cmenu = new javax.swing.JMenu("Center menu");
    javax.swing.JMenuItem citem = new javax.swing.JMenuItem("item");
    cmenu.add(citem);
    cbar.add(cmenu);

    javax.swing.JPanel panel = new javax.swing.JPanel();
    panel.setLayout(new java.awt.BorderLayout());
    panel.add(new javax.swing.JLabel("north label in JPanel"),
    java.awt.BorderLayout.NORTH);
    panel.add(cbar, java.awt.BorderLayout.CENTER);
    panel.setBorder(new javax.swing.border.LineBorder(
    java.awt.Color.RED));

    java.awt.Container pane = getContentPane();
    pane.setLayout(new java.awt.BorderLayout());
    pane.add(new javax.swing.JLabel("north label"),
    java.awt.BorderLayout.NORTH);
    pane.add(new javax.swing.JLabel("west label"),
    java.awt.BorderLayout.WEST);
    pane.add(panel, java.awt.BorderLayout.CENTER);
    pane.add(sbar, java.awt.BorderLayout.SOUTH);
    }

    public static void main(String[] args)
    {
    TestJMenuBar app = new TestJMenuBar();
    app.setSize(300, 300);
    app.setDefaultCloseOperation(EXIT_ON_CLOSE);
    app.setVisible(true);
    }
    }
    zero, Sep 24, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Niederberger

    Handle JMenuBar with visible glass pane

    Simon Niederberger, May 18, 2004, in forum: Java
    Replies:
    2
    Views:
    1,042
  2. Andreas Beresko

    Add a JPanel to a JPanel...

    Andreas Beresko, Jul 14, 2004, in forum: Java
    Replies:
    6
    Views:
    26,996
  3. Big - Goofy

    JInternalFrame and JMenuBar

    Big - Goofy, Jan 28, 2007, in forum: Java
    Replies:
    8
    Views:
    1,102
    Andrew Thompson
    Jan 30, 2007
  4. Raphael Jolivet

    [JPanel] Drop Down JPanel

    Raphael Jolivet, Jul 18, 2008, in forum: Java
    Replies:
    1
    Views:
    1,529
    John B. Matthews
    Jul 18, 2008
  5. t1m1976
    Replies:
    1
    Views:
    2,424
    t1m1976
    Nov 7, 2010
Loading...

Share This Page