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.
Related
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'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();
}
});
I have the following code for a JMenuBar (This code has been taken from a free java program call JGuiD and edited for personal purposes)
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.Dimension;
import java.awt.Color;
public class GuiDMenuBar extends JMenuBar
{
JMenu m_file,m_edit,m_help;
JMenuItem mi_f_new,mi_f_open,mi_f_save,mi_f_saveas,mi_f_exit;
JMenuItem mi_e_cut,mi_e_copy,mi_e_paste,mi_e_delete;
JMenuItem mi_v_motif,mi_v_java,mi_v_windows,mi_v_nimbus;
JMenuItem mi_h_help,mi_h_about;
JButton m_code;
public GuiDMenuBar()
{
setBorderPainted(true);
makeFileMenu();
makeEditMenu();
makeCodeButton();
makeHelpMenu();
}
void makeFileMenu()
{
m_file = new JMenu("File");
m_file.setMnemonic('F');
mi_f_new = new JMenuItem("New",new ImageIcon("icons/new_project.png"));
mi_f_new.setMnemonic('N');
mi_f_open = new JMenuItem("Open",new ImageIcon("icons/open_project.png"));
mi_f_open.setMnemonic('O');
mi_f_save = new JMenuItem("Save",new ImageIcon("icons/save.png"));
mi_f_save.setMnemonic('S');
mi_f_saveas = new JMenuItem("Save Java File",new ImageIcon("icons/saveas.png"));
mi_f_saveas.setMnemonic('J');
mi_f_exit = new JMenuItem("Exit",new ImageIcon("icons/exit.png"));
mi_f_exit.setMnemonic('X');
mi_f_new.setAccelerator(KeyStroke.getKeyStroke("control N"));
mi_f_open.setAccelerator(KeyStroke.getKeyStroke("control O"));
mi_f_save.setAccelerator(KeyStroke.getKeyStroke("control S"));
mi_f_saveas.setAccelerator(KeyStroke.getKeyStroke("control J"));
mi_f_exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,InputEvent.ALT_MASK));
m_file.add(mi_f_new);
m_file.add(mi_f_open);
m_file.addSeparator();
m_file.add(mi_f_save);
m_file.add(mi_f_saveas);
m_file.addSeparator();
m_file.add(mi_f_exit);
add(m_file);
}
void makeEditMenu()
{
m_edit = new JMenu("Edit");
m_edit.setMnemonic('E');
mi_e_cut = new JMenuItem("Cut",new ImageIcon("icons/cut.png"));
mi_e_cut.setMnemonic('X');
mi_e_copy = new JMenuItem("Copy",new ImageIcon("icons/copy.png"));
mi_e_copy.setMnemonic('C');
mi_e_paste = new JMenuItem("Paste",new ImageIcon("icons/paste.png"));
mi_e_paste.setMnemonic('P');
mi_e_delete = new JMenuItem("Delete",new ImageIcon("icons/delete.png"));
mi_e_delete.setMnemonic('D');
mi_e_cut.setAccelerator(KeyStroke.getKeyStroke("control X"));
mi_e_copy.setAccelerator(KeyStroke.getKeyStroke("control C"));
mi_e_paste.setAccelerator(KeyStroke.getKeyStroke("control V"));
mi_e_delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));
m_edit.add(mi_e_cut);
m_edit.add(mi_e_copy);
m_edit.add(mi_e_paste);
m_edit.add(mi_e_delete);
add(m_edit);
}
void makeHelpMenu()
{
m_help = new JMenu("Help");
m_help.setMnemonic('H');
mi_h_help = new JMenuItem("Help",new ImageIcon("icons/help.png"));
mi_h_help.setMnemonic('H');
mi_h_about = new JMenuItem("About");
mi_h_about.setMnemonic('A');
mi_h_help.setAccelerator(KeyStroke.getKeyStroke("F1"));
mi_h_about.setAccelerator(KeyStroke.getKeyStroke("control A"));
m_help.add(mi_h_help);
m_help.addSeparator();
m_help.add(mi_h_about);
add(m_help);
}
void makeCodeButton()
{
m_code = new JButton();
m_code.setOpaque(false);
m_code.setContentAreaFilled(false);
m_code.setBorder(null);
m_code.setFocusable(false);
m_code.setText("Code Shift C");
Dimension dBt = new Dimension(75,25);
m_code.setMinimumSize(dBt);
m_code.setPreferredSize(dBt);
m_code.setMaximumSize(dBt);
m_code.getModel().addChangeListener(new ChangeListener()
{
#Override
public void stateChanged(ChangeEvent e)
{
ButtonModel model = (ButtonModel) e.getSource();
if(model.isRollover())
{
m_code.setBackground(Color.RED);
m_code.setOpaque(true);
}
else
{
m_code.setBackground(null);
m_code.setOpaque(false);
m_code.setContentAreaFilled(false);
}
}
});
m_code.setMnemonic('C');
add(m_code);
}
public void addListeners(ActionListener al)
{
mi_f_new.addActionListener(al);
mi_f_open.addActionListener(al);
mi_f_save.addActionListener(al);
mi_f_saveas.addActionListener(al);
mi_f_exit.addActionListener(al);
mi_e_cut.addActionListener(al);
mi_e_copy.addActionListener(al);
mi_e_paste.addActionListener(al);
mi_e_delete.addActionListener(al);
mi_h_help.addActionListener(al);
mi_h_about.addActionListener(al);
m_code.addActionListener(al);
}
}
My aim is to make the JButton to appear like it is a JMenu. This would entail that the button only changes colour when I am interacting with the rest of the JMenuBar not just when I hover over it. For example if I had clicked on the JMenu m_file and then hovered over the JButton the background would change, however if I was not previously interacting with the JMenuBar it should not change the background when I hover over the JButton. The next thing required would be the JMenuBar treats it as a JMenu. I mean this in the sense that when F10 is clicked and the first JMenu is selected. After which you can then click on the right arrow on the arrow key pad on your keyboard, this will select the next JMenu. However using this method of navigation skips the JButton and does not allow you to interact with the JButton in anyway. I also mean this in the sense that if you interact with a JMenu and then hover over the JButton the JMenuBar shows you are still hovering over the JMenu as well (See Image).
So I suppose I have three questions.
How do you get the JButton to only change colour if the JMenuBar is already being interacted with?
How do you get the JMenuBar to treat the JButton as a JMenu in the senses that I described?
Is anyone aware of the exact colour of the background of the JMenu when you hover over it? Since I would prefer to change my JButton's background to the same colour of the JMenu's background instead of it just being red.
Thanks,
Dan
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.
I've got a JMenu and I want to change the window's content according to what button from the menu is pressed. I managed to show the panel as a popup, but I want it to be displayed in the same window with the menu. This is my code so far :
public class GUImenu extends JFrame
{
private JMenuBar menuBar;
private JMenu menu;
private JMenu subMenu;
private JMenuItem item1;
private JMenuItem item2;
private JMenuItem item3;
private JMenuItem item4;
private JMenuItem item5;
private JMenuItem item6;
public GUImenu()
{
super("Example Menu System");// Call the JFrame constructor.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Specify an action for the close button.
buildMenuBar();
// Pack and display the window.
pack();
setSize(1000, 250); // set frame size
setVisible(true);
}
private void buildMenuBar()
{
// Create the menu bar.
menuBar = new JMenuBar();
// Create the file and text menus.
menu = new JMenu("Menu"); menuBar.add(menu);
subMenu = new JMenu("Create Customer");
item1 = new JMenuItem("Ordinary Customer"); subMenu.add(item1);
item1.addActionListener(new showOrdinaryCust());
item6 = new JMenuItem("Privileged Customer"); subMenu.add(item6);
menu.add(subMenu);
item2 = new JMenuItem("View Customers Who Didn't Pay"); menu.add(item2);
item3 = new JMenuItem("Remove Client");menu.add(item3);
item4 = new JMenuItem("Create Order"); menu.add(item4);
item5 = new JMenuItem("Search..."); menu.add(item5);
setJMenuBar(menuBar);
}
public static void main(String[] args)
{
new GUImenu();
}
private class showOrdinaryCust implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==item1)
GUIpanel.main(null);
}
}
}
I would try to fill the entire window with a CardLayout. CardLayout is meant to switch its contents between separate views. Simply set up multiple cards for each of the panels you want to show and have the menu switch between them.
If you use windows or dialogs you will latter have to deal with the focus, the closing, minimizing, maximizing, re-size, centering, visibility...
In your case i would recommend you to pick a good layout to suit your needs(Probably the easiest way to achieve your goal).
What do you think about tabbed panes?
See this link: http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html
building guis is a little complex, but worth the time spent to understand what options are. This is a good place to start as it explains various java gui layouts, including using a layout manager. http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html.
For future posts, your example should be complete, including imports so we can copy and paste code, compile and look at.
It is simple enough. I have implemented this thing as follows :
First get content pane of your JFrame, say in container. Make this container object static.
private static Container container;
now get content pane.
container = this.getContentPane();
Now on click on menu call some method that will do some thing like this :
container.removeAll();
container.add(new JPanel()); //Add object of your panel you want to show.
container.revalidate();
This method is helpful in case you want to show multiple panels in same JFrame.