Results 1 to 9 of 9
  1. #1
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default Changing position of JMenuBar

    Hi! I wonder if it is possible to change the position of JMenuBar. I know that there two types of menu bars, i.e. vertical and horizontal. I'm using a horizontal one that is created by default. However, I want to locate it at 20px from the top. Is it possilbe to do?

  2. #2
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    why would you want to do that?
    one way could be to create a new panel and ad it to that panel, but it would be not very clean :p

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yep, as dinosoep states above, create a new JPanel, have it use BorderLayout, and add the JMenuBar to this JPanel BorderLayout.NORTH.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,239
    Rep Power
    19

    Default

    Quote Originally Posted by LianaN View Post
    I know that there two types of menu bars, i.e. vertical and horizontal.
    I'm curious. Where did you get that? I don't see it in the API.

    As far as your requirement is concerned, a JMenuBar is just another JComponent. You can nest your 'active' JMenuBar and another component in another JMenuBar with an appropriate layout, and set the latter to your JFrame.
    Java Code:
    import java.awt.BorderLayout;
    import javax.swing.*;
    
    public class NestedMenuBar {
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
          @Override
          public void run() {
            new NestedMenuBar().makeUI();
          }
        });
      }
    
      public void makeUI() {
        JMenu menu = new JMenu("Menu");
        menu.add(new JMenuItem("Item"));
        JMenuBar innerBar = new JMenuBar();
        innerBar.add(menu);
    
        JMenuBar outerBar = new JMenuBar();
        outerBar.setLayout(new BorderLayout());
        outerBar.add(Box.createVerticalStrut(20), BorderLayout.NORTH);
        outerBar.add(innerBar, BorderLayout.SOUTH);
    
        JFrame frame = new JFrame();
        frame.setJMenuBar(outerBar);
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    }
    db
    Last edited by DarrylBurke; 09-16-2010 at 11:19 PM.

  5. #5
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    Thank you. I''ll try right now.

  6. #6
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    I just mean that it is possible to position JMenu vertically or horizontally: Vertical menu bar : JMenuBar**Swing**Java Tutorial

    Your code works as I need. Thanks once again.

    BTW, I've tried with two JPanel and BorderLayout.NORTH/SOUTH, however it didn't work for me.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by LianaN View Post
    Your code works as I need. Thanks once again.
    BTW, I've tried with two JPanel and BorderLayout.NORTH/SOUTH, however it didn't work for me.
    Um,... Darryl's code is nothing more than using BorderLayout in a JPanel, and you've stated that it works, so I don't understand your last statement.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,239
    Rep Power
    19

    Default

    Your JPanels didn't have a preferredSize

    db

  9. #9
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    Hi guys. I've understood that for my case the best solution is to use JToolBar and then define toolbar.setFloatable(false), because I don't need any sub-menus (like JMenuItem).

Similar Threads

  1. Replies: 0
    Last Post: 08-14-2010, 10:09 AM
  2. JMenubar option
    By navishkumarb in forum New To Java
    Replies: 3
    Last Post: 05-24-2010, 02:03 AM
  3. Create JMenuBar using DataBase
    By arulmozs in forum AWT / Swing
    Replies: 1
    Last Post: 11-03-2009, 11:52 AM
  4. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 01:36 AM
  5. BorderFactory to set position?
    By aleplgr in forum AWT / Swing
    Replies: 2
    Last Post: 01-30-2008, 12:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •