0

I cant see my JMenu in the frame when I run it, what should i do?

I removed the panel where it was before, then i just want to put it in my frame

package app.ui;

import java.awt.Color;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
 import javax.swing.JSeparator;
 import javax.swing.WindowConstants;

 import app.model.User;
 import app.util.JMenusss;



 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseMotionAdapter;

 public class JMenus {
private JFrame menuu;
private SecurityQuestion securityQuestion;
private User user;

private JMenu mnAccount;

public JMenus(JFrame menuu) {
    this.menuu = menuu;
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {



    final JLabel lblHome = new JLabel("");
    lblHome.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent arg0) {
            Welcome myWelcome = new Welcome();
            menuu.dispose();
        }
    });
    lblHome.setIcon(new ImageIcon(JMenus.class.getResource("/app/resources/home-icon.png")));
    lblHome.setBounds(780, 4, 88, 83);
    menuu.getContentPane().add(lblHome);

    final JLabel lblItem = new JLabel("");
    lblItem.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent arg0) {
            ItemManagement myItemManagement = new ItemManagement();
            myItemManagement.ItemManagement();
            menuu.dispose();
        }
    });
    lblItem.setIcon(new ImageIcon(JMenus.class.getResource("/app/resources/items.png")));
    lblItem.setBounds(860, 4, 88, 83);
    menuu.getContentPane().add(lblItem);

    final JLabel lblGroupManagement = new JLabel("");
    lblGroupManagement.setIcon(new ImageIcon(JMenus.class.getResource("/app/resources/group11.png")));
    lblGroupManagement.setBounds(940, 4, 88, 83);
    menuu.getContentPane().add(lblGroupManagement);

    lblGroupManagement.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {
            GroupManagement myGroupManagement = new GroupManagement();
            myGroupManagement.groupManagement();

            menuu.dispose();
        }
    });


    final JLabel lblInventory = new JLabel("");
    lblInventory.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {
            IOStock myInventory = new IOStock();
            myInventory.InventoryWindow();
            menuu.dispose();
        }
    });
    lblInventory.setIcon(new ImageIcon(JMenus.class.getResource("/app/resources/IO.png")));
    lblInventory.setBounds(1020, 4, 88, 83);
    menuu.getContentPane().add(lblInventory);

    final JLabel lblLogout = new JLabel("");
    lblLogout.setIcon(new ImageIcon(JMenus.class.getResource("/app/resources/lock.png")));
    lblLogout.setBounds(1100, 4, 120, 83);
    menuu.getContentPane().add(lblLogout);
    lblLogout.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {

            int selectedOption = JOptionPane.showConfirmDialog(null,"You are about to logout, are you sure?","Choose",JOptionPane.YES_NO_OPTION); 
            if (selectedOption == JOptionPane.YES_OPTION) {
                Login window = new Login();
                window.frmLogin.setVisible(true);
                menuu.dispose();
            }
        }

    });

This is where my JMenu is

    JMenuBar mnbMenu = new JMenuBar();
    mnbMenu.setBackground(Color.WHITE);
    mnbMenu.setBounds(100, 4, 80, 89);
    menuu.getContentPane().add(mnbMenu);

    mnAccount = new JMenu();
    mnAccount.setBackground(Color.WHITE);
    mnAccount.setForeground(Color.WHITE);
    mnAccount.setIcon(new ImageIcon("/app/resources/Settings-icon.png"));
    mnAccount.setBounds(1180, 4, 100, 100);
    mnbMenu.add(mnAccount);



    JMenuItem mntmChangeUsername = new JMenuItem("Change Username");
    mntmChangeUsername.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent arg0) {
            UpdateUserName updateUsername = new UpdateUserName(user);
            updateUsername.setVisible(true);


        }
    });

    //mntmChangeUsername.setBackground(Color.WHITE);
    mnAccount.add(mntmChangeUsername);

    JMenuItem mntmChangePassword = new JMenuItem("Change Password");
    mntmChangePassword.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent arg0) {
            ChangeUsername changeUsername = new ChangeUsername(menuu);
            changeUsername.changeAcc();

        }
    });
    mntmChangePassword.setBackground(Color.WHITE);
    mnAccount.add(mntmChangePassword);

    JMenuItem mntmChangeSecurityQuestion = new JMenuItem("Change Security Question");
    mntmChangeSecurityQuestion.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent arg0) {
            ChangeSecurityQuestion changeSecurity = new ChangeSecurityQuestion(user, securityQuestion);
            changeSecurity.setVisible(true);
            changeSecurity.setLocationRelativeTo(null);
            changeSecurity.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            changeSecurity.setAlwaysOnTop(true);

        }
    });
    mntmChangeSecurityQuestion.setBackground(Color.WHITE);
    mnAccount.add(mntmChangeSecurityQuestion);






}

}

This is my code... I want to add the JMenu on my Frame,,, but its not visible, why?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
harraypotter
  • 111
  • 2
  • 2
  • 11
  • 2
    For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal Complete Tested and Readable Example). Note: Nowhere in those code snippets is a call to method `setJMenuBar`.. – Andrew Thompson Mar 15 '14 at 02:16
  • 1
    Tip: `mntmChangeUsername.addMouseListener(..` Don't add a mouse listener to a menu. It won't respond to keyboard input. Add an `ActionListener` or an `Action` instead. An `ActionListener`/`Action` will detect both mouse and keyboard input. – Andrew Thompson Mar 15 '14 at 02:18
  • 1
    Tip 2: `lblInventory.setBounds(1020, 4, 88, 83);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Mar 15 '14 at 02:19

1 Answers1

2

Here:

JMenuBar mnbMenu = new JMenuBar();
...
menuu.getContentPane().add(mnbMenu);

The correct way to set the menu bar to a JFrame is through setJMenuBar() method:

JMenuBar mnbMenu = new JMenuBar();
...
menuu.setJMenuBar(mnbMenu);

Take a look to How to Use Menus tutorial. Additionaly you may want to see this topic Why JMenuBar is not place in the JFrame content pane(...)

Side note

Take a look to all @AndrewThompson's tips:

  • MCTaRE (Minimal Complete Tested and Readable Example)
  • Nowhere in those code snippets is a call to method setJMenuBar
  • Don't add a mouse listener to a menu. Add an ActionListener or an Action instead.
  • To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space.
Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • *"Take a look to both @AndrewThompson's tips."* There were 3. The first was hidden at the end of the first comment. ;) – Andrew Thompson Mar 15 '14 at 04:36
  • Sorry I totally overlooked that one! Just fixed :) @AndrewThompson – dic19 Mar 15 '14 at 12:17
  • *"Sorry I totally overlooked that one!"* No! I was referring more specifically to: *"Note: Nowhere in those code snippets is a call to method `setJMenuBar`"* but then, your answer does cover that more comprehensively and better. ;) – Andrew Thompson Mar 15 '14 at 23:24
  • 1
    Aaaah that part! Honestly I didn't finish to read your first comment entirely :P What a shame! When I was starting to read I've assumed (mistakenly) that your comment was completely about MCTaRE. Again sorry about that. @AndrewThompson – dic19 Mar 15 '14 at 23:37
  • 1
    Gee.. now you made me want to up-vote the answer ..again. :) – Andrew Thompson Mar 15 '14 at 23:41
  • 1
    Hehe thanks! BTW I never told you this before but those Q&A you always quote are outstanding! The one about white spaces deserves much more up-votes, seriously. I can't up-vote them again but I'd like to. @AndrewThompson – dic19 Mar 15 '14 at 23:51
  • 2
    *"..those Q&A you always quote are outstanding!"* When I'm asking the question with intentions of answering it, it means I can write a great (IMO) answer. That is one of those things that most other people tend to forget when answering a 'how do I put my components exactly where I want?' type question. Enough inbound links, and it will 'make it' as far as votes go. It is just a matter of time and patience. ;) – Andrew Thompson Mar 15 '14 at 23:59