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