0

I am facing a problem with my JMenuBar location in my Java application.

I am actually using ComponentResizer.java code from the article.

and everything with the resizing works fine except from the north area of my application (undecorated JFrame) and its corners (of North Area) because the JMenuBar is preventing me from resizing from that area.

Is there a solution or maybe a hack to move the JMenuBar down a little or enable the Resizing in the north area?

I also use setJMenuBar() method to add the JMenuBar to the north area of my application.

Code:

  public class MyApp extends JFrame {

private MyApp frame;
private JMenuBar menuBar;

public static void main(String[] args) {
    frame = new MyApp();
    frame.setUndecorated(true);
    frame.setVisible(true);
}
public MyApp(){
    initComponents();
}
private void initComponents(){
    setTitle("MediaForm");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 673, 482);
    menuBar = new JMenuBar();
    setJMenuBar(menuBar);
}
}
PeGiannOS
  • 227
  • 4
  • 19
  • Please edit your question to include an [sscce](http://sscce.org/) that exhibits the problem you describe. No need to repeat `ComponentResizer`. – trashgod Sep 06 '12 at 21:00

2 Answers2

1

I've not tested this, but JRootPane is a JComponet, it may be possible to add a EmptyBorder to it, allowing the JMenuBar to be offset.

Failing that, you'll probably need to implement your own JRootPane to manage the layout of the JMenuBar & content pane

UPDATE with Testing

public class TestMenuFrame extends JFrame {

    public TestMenuFrame() throws HeadlessException {

        setTitle("Test");

        getRootPane().setBorder(new EmptyBorder(10, 10, 10, 10));

        JMenuBar mb = new JMenuBar();
        mb.add(new JMenu("Test"));

        setJMenuBar(mb);

        setSize(100, 100);

        getContentPane().setBackground(Color.RED);

        setLocationRelativeTo(null);

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        new TestMenuFrame().setVisible(true);

    }
}

Sample

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I don't think that this is my problem.I just have no access to JFrame listeners in the north area of my application – PeGiannOS Sep 07 '12 at 08:03
  • And I use an undecorated frame.Thanks for the quick answer – PeGiannOS Sep 07 '12 at 08:16
  • Firstly, if you can add components to any part of the frame, you only need to walk the component hierarchy until you reach the JRootPane, besides which, you stated you used setJMenu, meaning you must have access to the frame. Secondary, I hardly think using an undecorated frame will make any difference to the solution I've suggested – MadProgrammer Sep 08 '12 at 04:39
1

JMenuBar extends JComponent, therefore you can put it anywhere you would put an ordinary component. Note that it could be confusing to the users if you put it to unexpected locations.

Also see this: add JMenuBar to a JPanel?

Community
  • 1
  • 1
lbalazscs
  • 17,474
  • 7
  • 42
  • 50