JMenu disappears when JRadioButtons or JCheckBoxes are used - java

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.

Related

Trying to create a MenuBar using Java.swing, and created an object of JMenu(), but it wont let me add It to the frame and not sure why?

public class Menu {
private JMenu borders;
//constructor
public Menu()
{
//crate object
//this.menuBar = new JMenuBar();
//------create menu items----
//menu item for borders
borders = new JMenu("Border");
// add border options
borders.setMnemonic(KeyEvent.VK_U);
//add border options
borders.add(new JMenuItem("Etched"));
borders.add(new JMenuItem("Raised"));
borders.add(new JMenuItem("Matte"));
borders.add(new JMenuItem("Tilted"));
borders.add(new JMenuItem("Compounded"));
JMenuItem quit = new JMenuItem("Quit");
quit.setMnemonic(KeyEvent.VK_Q);
borders.add(quit);
//set Accelerator
quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, Event.CTRL_MASK));
//add action listener
//#Override
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
}
public class DrawingFrame extends JFrame{
protected CanvasEditor canvasEditor;
protected DrawingCanvas drawingCanvas;
private Menu menu;
public DrawingFrame()
{
this.setTitle("Drawing Application");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
drawingCanvas = new DrawingCanvas();
drawingCanvas.setMinimumSize(new Dimension(300,700));
this.add(drawingCanvas);
JPanel toolBarPanel = getToolBarPanel();
this.add(drawingCanvas,BorderLayout.CENTER);
this.add(toolBarPanel,BorderLayout.SOUTH);
//create menu from class
this.menu = new Menu();
//create menu bar
JMenuBar menuBar = new JMenuBar();
//add menu to menuBar
menuBar.add(menu);
error: cannot resolve method 'add'
not sure why it won't accept the add method
The drawingFrame class accepts objects from canvasEditor and DrawingCanvas but it doesn't accept objects from Menu class.
I want to create a menuBar and add a menu to it, and then have it on the same frame -- drawing Frame
pretty sure it's a simple fix, but not sure how to fix it
public class Menu {
private JMenu borders;
You create a class called Menu and create an instance of a JMenu in this class.
this.menu = new Menu();
//create menu bar
JMenuBar menuBar = new JMenuBar();
//add menu to menuBar
menuBar.add(menu);
You then create an instance of the Menu class and attempt to add it to the JMenuBar, which can't be done because the Menu class is NOT a Component.
Instead you want to add the borders variable to the menubar.
This means you need to create a method in your Menu class like:
public JMenu getJMenu()
{
return borders;
}
Then you use:
//menuBar.add(menu);
menuBar.add( menu.getJMenu() );

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.

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

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

Categories

Resources