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

23,160

Solution 1

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);
    }
}

Solution 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.

Solution 3

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

Share:
23,160
Frizinator
Author by

Frizinator

Programmer aspiring to own a software company one day. Definitely have learned a lot throughout the years and still learning more! Java is my specialty.

Updated on December 18, 2020

Comments

  • Frizinator
    Frizinator over 3 years

    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.