Creating a Drop Down Menu Bar in Java GUI - java

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

Related

Mnemonic working for MenuBar but not MenuItems

My program has a menu bar with one menu one it called "File." Inside the file menu there are 4 options: "New", "Pause", "Unpause", and "Exit". All five of these have mnemonics assigned however only the one for File works the way I had hoped.
The four others work, but they only work if I activate the mnemonic for File first. i.e. To activate "New" I need to press Alt+F, the Alt+N. I didn't think mnemonics were suppose to work that way, but I could be mistaken.
This is the code I currently have, maybe someone can point out what I'm doing wrong.
//MENU BAR
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem newGame;
private JMenuItem pauseGame;
private JMenuItem unpauseGame;
private JMenuItem exitGame;
//CREATE THE FILE MENU
public void buildMenuBar(){
//INITIAILIZE
menuBar = new JMenuBar();
//BUILD FILE MENU
buildFileMenu();
//ADD TO MENU BAR
menuBar.add(fileMenu);
//SET
setJMenuBar(menuBar);
}
public void buildFileMenu(){
//INITIALIZE
fileMenu = new JMenu("File");
newGame = new JMenuItem("New");
pauseGame = new JMenuItem("Pause");
unpauseGame = new JMenuItem("Unpause");
exitGame = new JMenuItem("Exit");
//MNEMONICS
fileMenu.setMnemonic(KeyEvent.VK_F);
newGame.setMnemonic(KeyEvent.VK_N);
pauseGame.setMnemonic(KeyEvent.VK_P);
unpauseGame.setMnemonic(KeyEvent.VK_U);
exitGame.setMnemonic(KeyEvent.VK_X);
//LISTENERS
newGame.addActionListener(new MenuListener());
exitGame.addActionListener(new MenuListener());
//ADD TO FILEMENU
fileMenu.add(newGame);
fileMenu.add(pauseGame);
fileMenu.add(unpauseGame);
fileMenu.add(exitGame);
}
So I'm going to answer my own question. I've learned that mnemonics like I was trying to use only work when the menu is active. That's why they worked for the "File" option but not the "New Game" option unless the file option was already open. It was working right, just not how I understood it to work.

I want JMenu to show the items when the program starts

I've created a menubar and added a menu with several items. When I do doClick() on the JMenu(archiveMenu) it highlights the button for the menu but it doesnt show any of the items that are added to it. I've tried doing doClick() before the adding of actionListeners to the items and setVisible(true) but nothing works.
I think it has something to do with the fact that they have seperate actionListeners but I'm so far in to the program that changing to a single actionListener would lead to a huge amount of work. I appreciate any help, thanks!
It looks like this:
// MENU BAR
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu archiveMenu = new JMenu("Archive");
menuBar.add(archiveMenu);
JMenuItem newItem = new JMenuItem("New Map");
JMenuItem loadItem = new JMenuItem("Load places");
JMenuItem saveItem = new JMenuItem("Save");
JMenuItem exitItem = new JMenuItem("Exit");
newItem.addActionListener(new NewMapLis());
loadItem.addActionListener(new LoadLis());
saveItem.addActionListener(new SaveLis());
exitItem.addActionListener(new ExitLis());
archiveMenu.add(newItem);
archiveMenu.add(loadItem);
archiveMenu.add(saveItem);
archiveMenu.add(exitItem);
archiveMenu.doClick();
What is happening is that when doClick() is called the window is not fully loaded yet.
you need to perform it after loading.
for example, you could call it when the frame is opened:
myFrame.addWindowListener(new WindowAdapter() {
#Override
public void windowOpened(WindowEvent e) {
archiveMenu.doClick();
}
});

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

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

JMenu disappears when JRadioButtons or JCheckBoxes are used

I have created a JMenuBar with a Pizza JMenu. The JMenu has 3 JRadioButtons and 2 JCheckBoxes that appear when menuItems are scrolled over. The problem is that when I click on a JRadioButton or JCheckBox, the whole menu disappears. Is there a particular method or any other way I can stop this from happening?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MenuExamplePizza extends JFrame
{
private JMenuBar menuBar;
private JMenu menu, sizeMenu, extrasMenu, helpMenu;
private JMenuItem menuItem;private JRadioButtonMenuItem smallrbMenuItem, medrbMenuItem, largerbMenuItem;
private JCheckBoxMenuItem peppchMenuItem, anchMenuItem;
public MenuExamplePizza() // Default Constructor
{
menuBar = new JMenuBar(); //Create the menu bar
menu = new JMenu("Pizza"); //Build the first menu
menu.setMnemonic('P');
menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
menuBar.add(menu);
menuItem = new JMenuItem("Total");
menuItem.setMnemonic('T');
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK));
menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
menu.add(menuItem);
menu.addSeparator();
sizeMenu = new JMenu("Size");
sizeMenu.setMnemonic('S');
ButtonGroup group = new ButtonGroup();
smallrbMenuItem = new JRadioButtonMenuItem("Small");
group.add(smallrbMenuItem);
sizeMenu.add(smallrbMenuItem);
medrbMenuItem = new JRadioButtonMenuItem("Medium");
group.add(medrbMenuItem);
sizeMenu.add(medrbMenuItem);
largerbMenuItem = new JRadioButtonMenuItem("Large");
group.add(largerbMenuItem);
sizeMenu.add(largerbMenuItem);
menu.add(sizeMenu);
extrasMenu = new JMenu("Extras");
extrasMenu.setMnemonic('E');
peppchMenuItem = new JCheckBoxMenuItem("Pepperoni");
extrasMenu.add(peppchMenuItem);
anchMenuItem = new JCheckBoxMenuItem("Anchovies");
extrasMenu.add(anchMenuItem);
menu.add(extrasMenu);
menu.addSeparator(); // a group of radio button menu items
menuItem = new JMenuItem("Exit");
menuItem.setMnemonic('x');
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
menu.add(menuItem);
helpMenu = new JMenu("Help"); //Build second menu in the menu bar.
menuBar.add(menu);
menuBar.add(helpMenu);
add(menuBar);
setJMenuBar(menuBar);
}
Check out Keeping Menus Open to see if it does what you want.

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

Categories

Resources