Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 975
Frizinator Avatar asked Jul 21 '12 03:07

Frizinator


2 Answers

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);
    }
}
like image 73
tenorsax Avatar answered Oct 02 '22 22:10

tenorsax


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.

like image 24
Kumar Vivek Mitra Avatar answered Oct 02 '22 22:10

Kumar Vivek Mitra