Java - Is it possible to add a JMenuBar to a JFrame's decoration window?
Asked Answered
C

3

6

I'm wondering if I can add a JMenuBar to a JFrame or JRootPane's Decoration window, or otherwise the border that surrounds the content pane within. I see applications like Firefox or Photoshop having their menubar in the decoration window.

Is this possible? I've looked around google, but I haven't been able to find any results over this kind of thing. I'm hoping Java has this capability.

Complex answered 21/7, 2012 at 3:59 Comment(3)
Yes, I've been going through the API to find an answer with no luck.Complex
Unfortunatelly I think you can't do this.Frankfurter
I don't think what you want to do can be achieved without native support, unless you're willing to provide your own frame decorationMissi
M
9

Not sure what you're looking for, but you can add JMenuBar to JFrame - JFrame.setJMenuBar(). Look at How to Use Menus tutorial for details.

Edit:

Below is an overly simplified example of undecorated frame with a menu, just to demo the idea.

You may want to turn to existing solutions - JIDE has ResizableFrame for this purpose. It is part of open source JIDE-oss. Substance L&F has support for title bar customization (see What happened to the Substance LaF?). You can also very efficiently utilize ComponentMover and ComponentResizer classes by @camickr, see Resizing Components article for more details.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class UndecoratedFrameDemo {
    private static Point point = new Point();

    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setUndecorated(true);
        frame.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                point.x = e.getX();
                point.y = e.getY();
            }
        });
        frame.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                Point p = frame.getLocation();
                frame.setLocation(p.x + e.getX() - point.x,
                        p.y + e.getY() - point.y);
            }
        });

        frame.setSize(300, 300);
        frame.setLocation(200, 200);
        frame.setLayout(new BorderLayout());

        frame.getContentPane().add(new JLabel("Drag to move", JLabel.CENTER),
                BorderLayout.CENTER);

        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        menuBar.add(menu);
        JMenuItem item = new JMenuItem("Exit");
        item.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        menu.add(item);
        frame.setJMenuBar(menuBar);

        frame.setVisible(true);
    }
}
Manage answered 21/7, 2012 at 4:7 Comment(3)
What I am looking for is that the MenuBar is located in the actual border of the window. If you look at photoshop's GUI, the File/Edit/Etc menu's are in the Window's Border. Instead of the Menu bar using it's own space, it utilizes the windows border/decoration space.Complex
Well, you can remove frame decoration and create your own - setUndecorated(true)Manage
@StevenTylerFrizell I included an example of undecorated frame.Manage
C
2

Try this...

Adding JMenuBar to the JFrame

JMenuBar menuBar = new JMenuBar();
myFrame.setJMenuBar(menuBar);

Adding JMenuBar to the JPanel

JMenuBar menuBar = new JMenuBar();
myPanel.add(menuBar);

See this JMenuBar Tutorial link for more information on the topic.

Chronogram answered 21/7, 2012 at 4:49 Comment(0)
M
1

I've done some digging and the short answer is ... no.

Basically the frame decoration is off loaded to the OS, so we don't have any access to it's content.

However, if you want to go to a lot of work, you can implement your own decoration. You need to take responsibility for resizing and moving though.

You might want to check out

http://java-swing-tips.blogspot.com.au/2010/05/custom-decorated-titlebar-jframe.html and http://www.paulbain.com/2009/10/13/howto-draggabe-jframe-without-decoration/ for ideas

Missi answered 22/7, 2012 at 4:6 Comment(1)
Another idea I had was to use the JInternalFrame as bases for the decoration, maybe stealing the buttons for exampleMissi

© 2022 - 2024 — McMap. All rights reserved.