dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Java Swing: Menu Bar

  1. #1
    Join Date
    Jul 2016
    Posts
    2

    Java Swing: Menu Bar

    Hello!

    I have the next code.

    first file
    Code:
    public class Main
    {    
        public static void main(String[] args)
        {    
            new MyFrame();
        }
    }

    second file
    Code:
    public class MyFrame extends JFrame
    {
        private MyMenuBar menu;
        private Toolbar toolbar;
        public MyFrame()
        {
            menu = new MyMenuBar();
            toolbar = new Toolbar();
            
            setJMenuBar(menu);
            
            setExtendedState(JFrame.MAXIMIZED_BOTH);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setIconImage(new ImageIcon("C:/Users/Stelian/Documents/NetBeansProjects/Main/src/main/images/apps.png").getImage());
            setVisible(true);
        }
    }
    third file
    Code:
    public class MyMenuBar extends JMenuBar
    {
        public JMenu File, Books, Members, Search, Loan, Help;
        
        public JMenuItem Exit;
        public JMenuItem AddBook, ListBooks;
        public JMenuItem AddMembers, ListMembers;
        public JMenuItem SearchBooksAndMembers;
        public JMenuItem BorrowBook, ReturnBook;
        public JMenuItem About;
        
        public JMenuBar menuBar = new JMenuBar();
    
        public MyMenuBar()
        {
            File = new JMenu("File");
            File.setMnemonic('F');
            
            Books = new JMenu("Books");
            Books.setMnemonic('B');
            
            Members = new JMenu("Members");
            Members.setMnemonic('M');
            
            Search = new JMenu("Search");
            Search.setMnemonic('S');
            
            Loan = new JMenu("Loan");
            Loan.setMnemonic('L');
            
            Help = new JMenu("Help");
            Help.setMnemonic('H');
    
            Exit = new JMenuItem("Exit", new ImageIcon("C:/Users/Stelian/Documents/NetBeansProjects/Main/src/main/images/exit.png"));
            File.add(Exit);
            Exit.setMnemonic(KeyEvent.VK_E);
            Exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,ActionEvent.ALT_MASK));
    
            AddBook = new JMenuItem("Add Books", new ImageIcon("C:/Users/Stelian/Documents/NetBeansProjects/Main/src/main/images/add.png"));
            Books.add(AddBook);
            AddBook.setMnemonic(KeyEvent.VK_B);
            AddBook.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,ActionEvent.CTRL_MASK));
            
            ListBooks = new JMenuItem("List All Books", new ImageIcon("C:/Users/Stelian/Documents/NetBeansProjects/Main/src/main/images/print.png"));
            Books.add(ListBooks);
            ListBooks.setMnemonic(KeyEvent.VK_L);
            ListBooks.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,ActionEvent.CTRL_MASK));
            
            AddMembers = new JMenuItem("Add Members", new ImageIcon("C:/Users/Stelian/Documents/NetBeansProjects/Main/src/main/images/add.png"));
            Members.add(AddMembers);
            AddMembers.setMnemonic(KeyEvent.VK_A);
            AddMembers.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,ActionEvent.CTRL_MASK));
            
            ListMembers = new JMenuItem("List All Members", new ImageIcon("C:/Users/Stelian/Documents/NetBeansProjects/Main/src/main/images/print.png"));
            Members.add(ListMembers);
            ListMembers.setMnemonic(KeyEvent.VK_L);
            ListMembers.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,ActionEvent.CTRL_MASK));
            
            SearchBooksAndMembers = new JMenuItem("Search", new ImageIcon("C:/Users/Stelian/Documents/NetBeansProjects/Main/src/main/images/search.png"));
            Search.add(SearchBooksAndMembers);
            SearchBooksAndMembers.setMnemonic(KeyEvent.VK_S);
            SearchBooksAndMembers.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,ActionEvent.CTRL_MASK));
            
            BorrowBook = new JMenuItem("Borrow Books", new ImageIcon("C:/Users/Stelian/Documents/NetBeansProjects/Main/src/main/images/borrow.png"));
            Loan.add(BorrowBook);
            BorrowBook.setMnemonic(KeyEvent.VK_B);
            BorrowBook.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,ActionEvent.CTRL_MASK));
            
            ReturnBook = new JMenuItem("Return Books", new ImageIcon("C:/Users/Stelian/Documents/NetBeansProjects/Main/src/main/images/return.png"));
            Loan.add(ReturnBook);
            ReturnBook.setMnemonic(KeyEvent.VK_R);
            ReturnBook.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,ActionEvent.CTRL_MASK));
            
            About = new JMenuItem("About", new ImageIcon("C:/Users/Stelian/Documents/NetBeansProjects/Main/src/main/images/about.png"));
            Help.add(About);
            About.setMnemonic(KeyEvent.VK_A);
            About.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,ActionEvent.CTRL_MASK));
        }
    }
    The code doesn't show my menubar.
    Can anyone tell me why?
    Thank you!

  2. #2
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: Java Swing: Menu Bar

    Where are you adding the menu options (File, Book, etc) in MyMenuBar?

    Please follow the Java naming conventions more consistently (variable/method names start in a lowercase letter).

  3. #3
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,856

    Re: Java Swing: Menu Bar

    Another possible problem is that the code has 2 menu bar object. See code with <<<<<<<:
    Code:
    public class MyMenuBar extends JMenuBar   <<<<<<<<<< MyMenuBar is a JMenuBar
    {
        public JMenu File, Books, Members, Search, Loan, Help;
        
        public JMenuItem Exit;
        public JMenuItem AddBook, ListBooks;
        public JMenuItem AddMembers, ListMembers;
        public JMenuItem SearchBooksAndMembers;
        public JMenuItem BorrowBook, ReturnBook;
        public JMenuItem About;
        
        public JMenuBar menuBar = new JMenuBar();    <<<<<<<<<<<< another JMenuBar
    Norm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)