Why isn't my 'Menu' bar displaying in my JFrame 'Window' Object? - java

Here I've created the JFrame object, 'window'. And set it to 'true'.
import java.awt.*;
public static void main(String[] args) {
JFrame window = new JFrame("GUI Test");
window.setSize(250, 100);
window.setLocation(100, 100);
window.setVisible(true);
Here I've added menu items to the 'menu'. And set visibility as 'true'.
JMenuItem home = new JMenuItem("Home");
JMenuItem about = new JMenuItem("About");
JMenuItem tag = new JMenuItem("Tag");
JMenu menu = new JMenu("menu");
menu.add(home);
menu.add(about);
menu.add(tag);
menu.setVisible(true);
}
I'm not getting any errors. Then what am I missing? Why doesn't my menu display in the 'window' object?

You need to add your JMenus to a JMenuBar and set to to the JFrame using JFrame#setJMenuBar
See How to use Menus for more details

Frirst creat JMenubar and add the JMenu into that.
JMenuBar menubar = new JMenuBar();
Add the JMenu into JMenuBar and JMenuar into JFrame to display in the window.
menubar.add(menu);
window.setJMenuBar(menubar);

You need to add JMenuBar with window,
JMenuBar menuBar= new JMenuBar();
.....
menuBar.add(menu);//Add menu with JMenuBar.
window.setJMenuBar(menuBar);

Add the Jmenu into in you JFrame
window.add(menu);
and also set the layout to JFrame
window.setLayout(new Flawlayout()); // you can try other layout too
And best way is
How to use Menus

Related

Is there a way to move a jMenuBar to somewhere else than the top of my jFrame?

I'm creating an undecorated jFrame, and I wanted to add a jMenuBar to this jFrame to allow me to use drop-down menus for various things.
But, the jMenuBar always defaults to the very top of my jFrame, and it being undecorated, it pushes down a jPanel I had set there with the "Minimize" and "Close" buttons, and trying to move it anywhere else is unsuccessful.
I've tried using a jToolBar, and while it allows for more customization in terms of placement, I can't find a way to add drop-down menus onto the buttons there.
Is there a way to move a jMenuBar somewhere else than the top or to add a drop-down menu to a jToolBar?
You should have a look at Layout Managers.
Here is a simple example to achieve what you want, using a BorderLayout:
public static void main(String[] args) {
JFrame jf = new JFrame();
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
JLabel label = new JLabel("North");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("A south menu");
menu.add(new JMenuItem("A south item"));
menuBar.add(menu);
container.add(label, BorderLayout.NORTH);
container.add(menuBar, BorderLayout.SOUTH);
jf.add(container);
jf.setPreferredSize(new Dimension(200,100));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.pack();
}
result:

Creating a Drop Down Menu Bar in Java GUI

I'm practicing creating a GUI program in Java using swing and awt imports. Most everything in my main class is working, except for getting a drop-down menu with a tab named file at the top of the GUI. I have two snippets of code, one in the JPanel class and the other in the public main() class. I ultimately want to get a file menu with save and save as options at the top. Don't need to invoke anything or add listeners, just to make them visible on the program itself. I'm using JMenuBar menuBar along with JMenu fileMenu to create it. What am I doing wrong? Snippets below:
JMenuBar menuBar = new JMenuBar();
JMenuItem saveItem, saveAllItem;
JMenuItem menuItem = new JMenuItem("Save");
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
saveItem = fileMenu.add("Save");
saveAllItem = fileMenu.add("Save All");
panel.add(menuItem);
setVisible(true);
There is no any need of adding JMenuBar object to JPanel because it is only linked with the JFrame. You need to pass the JMenuBar object to JFrame method setJMenuBar() in order to set the menu bar in the window.You can create a drop down menu for JFrame by using this code:
JMenuBar menuBar = new JMenuBar();
JMenuItem saveItem, saveAllItem;
// Menu
JMenu fileMenu = new JMenu("File");
// Menu Item (Drop down menus)
saveItem = new JMenuItem("Save");
saveAllItem = new JMenuItem("Save All");
// Adding menu items to menu
fileMenu.add(saveItem);
fileMenu.add(saveAllItem);
// adding menu to menu bar
menuBar.add(fileMenu);
// setting menubar at top of the window.
// if you create a object of JFrame in class then code to set JMenuBar to JFrame will be:
// jframe.setJMenuBar(menuBar);
// if class is extending JFrame then it will be like this:
setJMenuBar(menuBar);

java.lang.NullPointerException with JMenu and JMenuBar

I have a JMenuBar that has one menu and three JRadioButtonMenuItems:
JMenuBar menuBar;
JMenu menu = new JMenu("Menu");
JRadioButtonMenuItem rbMenuItem;
I declare it:
menu = new JMenu("A Menu");
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
rbMenuItem.setSelected(true);
group.add(rbMenuItem);
menu.add(rbMenuItem);
menuBar.add(menu);
and I set it as the menu bar:
this.setJMenuBar(menuBar);
I run the file and it gives me:
Exception in thread "main" java.lang.NullPointerException
at geometry.tools.main.RectangleFrame.<init>(RectangleFrame.java:47)
at geometry.tools.main.RectangleFrame.main(RectangleFrame.java:95)
Java Result: 1
Line 47 is:
menuBar.add(menu);
and line 95 is:
RectangleFrame thr = new RectangleFrame();
in the:
public static void main(String[] args){
RectangleFrame thr = new RectangleFrame();
}
I don't really understand why. I haven't specified any value as null so I don't see why I get this error.
Thanks a lot.
menuBar is not initialized
JMenuBar menuBar = new JMenuBar();
You have to create an instance of your menubar.
JMenuBar menuBar = new JMenuBar()

How do I resize my JMenuItem to fit a JButton?

Here is the code I am using:
JMenu menu = new JMenu("Menu");
JMenuItem item = new JMenuItem("Add");
item.add(new JButton("SOMETHING A BIT WORDY"));
menu.add(item);
// ...
JMenuBar menuBar = new JMenuBar();
menuBar.addMenu(menu);
JFrame frame = new JFrame();
frame.setJMenuBar(menuBar);
// ...
frame.pack();
frame.setVisible();
However, the button (inside of the menu item) appears small and only contains the text "..." which is used when the button's size cannot fit the intended text. Is there any way to make my JMenuItem "grow" to fit my JButton (or JTextArea, or JLabel, or whatever the Component may be)?
I totally agree with others who posted their comments, it may be an odd mix to put an button in the menuitem, if you insist to this, setPreferredSize can solve your problem.
I used a bad hard code implementation, if anyone know how to set the width dynamically, please guide me too. :P thx in advance.
JMenu menu = new JMenu("Menu");
JMenuItem item = new JMenuItem("Add");
item.add(new JButton("SOMETHING A BIT WORDY"));
item.setLayout(new FlowLayout()); // set FlowLayout for item
item.setPreferredSize(new Dimension(200, 100)); // hard code implementation :-(
menu.add(item);
// ...
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
JFrame frame = new JFrame();
frame.setJMenuBar(menuBar);
// ...
frame.pack();
frame.setVisible(true);

Java GUI menu bar not showing

Any idea why the menu bar menuBar is not showing? everything looks fine to me.
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class mySticky extends JFrame implements ActionListener{
//weStart!
JFrame frame = new JFrame("Sticky Note");
JMenuBar menuBar = new JMenuBar();
JMenu noteMenu = new JMenu("Note");
JMenuItem newNote = new JMenuItem("New Note");
JMenuItem open = new JMenuItem("Open");
JMenuItem saveAs = new JMenuItem("Save As");
JMenuItem save = new JMenuItem("Save");
//Constructor
public mySticky(){
setSize(400,300);
setLocation(500,250);
setTitle("Sticky Note");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
menuBar.add(noteMenu);
noteMenu.add(newNote);
noteMenu.add(open);
noteMenu.add(saveAs);
noteMenu.add(save);
frame.setJMenuBar(menuBar);
}
public void actionPerformed (ActionEvent e){
}
public static void main (String [] args ){
mySticky sticky = new mySticky ();
sticky.setVisible(true);
}
}
You add the menubar to frame, which is never added to any UI. Replace
frame.setJMenuBar(menuBar);
by
setJMenuBar(menuBar);
and your menubar will become visible. Or you should add frame to the UI as well. Not sure what you tried to achieve.
And you should wrap the code of your main method in a Runnable and execute it on the EDT (for example using EventQueue.invokeLater)
Instead of frame.setJMenuBar(menuBar), try this.setJMenuBar(menuBar) in your constructor.

Categories

Resources