Results 1 to 4 of 4

Thread: How to create Menus in Java Application?

  1. #1
    Join Date
    May 2008
    Posts
    2,297

    How to create Menus in Java Application?

    Hi, can anyone give me the code to develop the Menus in java for application. As, I am making my project in java and have many more buttons on one form, so want to use menus for making look and feel. So, can you help me to do so. In our college we don't get any code regarding this. That is why I am not able to do this.

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to create Menus in Java Application?

    Hi, you can make use of the following code to make Menus In java Application.

    Code:
    import java.awt.*; 
    import java.awt.event.*; 
     
    public class Menu extends Frame
     { 
      public Menu() 
    { 
        super("Menu Window In Java"); 
        setSize(200, 200); 
        FileMenu fmenu = new FileMenu(this); 
        HelpMenu helpMenu = new HelpMenu(this); 
        MenuBar mbar = new MenuBar(); 
        mbar.add(fmenu); 
        mbar.add(helpMenu); 
        setMenuBar(mbar); 
        addWindowListener(new WindowAdapter()
     { 
          public void windowClosing(WindowEvent e)
     { 
            exit(); 
          } 
        }); 
      } 
     
      public void exit()
     { 
        setVisible(false); 
        dispose(); 
        System.exit(0); 
      } 
     
      public static void main(String args[]) 
    { 
        Menu w = new Menu(); 
        w.setVisible(true); 
      } 
    } 
     
    class FileMenu extends Menu implements ActionListener
     { 
      Menu mw;  
      public FileMenu(Menu m) 
    { 
        super("File"); 
        mw = m; 
        MenuItem mi; 
        add(mi = new MenuItem("Open")); 
        mi.addActionListener(this); 
        add(mi = new MenuItem("Close")); 
        mi.addActionListener(this); 
        add(mi = new MenuItem("Exit")); 
        mi.addActionListener(this); 
      } 
     
      public void actionPerformed(ActionEvent e) 
    { 
        String item = e.getActionCommand(); 
        if (item.equals("Exit"))  
          mw.exit(); 
        else  
          System.out.println("Selected FileMenu " + item); 
      } 
    } 
     
    class HelpMenu extends Menu implements ActionListener 
    { 
      Menu mw;  
      public HelpMenu(Menu m)
     { 
        super("Help"); 
        mw = m; 
        MenuItem mi; 
        add(mi = new MenuItem("First")); 
        mi.addActionListener(this); 
        add(mi = new MenuItem("Advanced")); 
        mi.addActionListener(this); 
        addSeparator(); 
        add(mi = new CheckboxMenuItem("Manual")); 
        mi.addActionListener(this); 
           Menu subMenu = new Menu("Miscellaneous"); 
        subMenu.add(mi = new MenuItem("Help")); 
        mi.addActionListener(this); 
        subMenu.add(mi = new MenuItem("Other Option")); 
        mi.addActionListener(this); 
        add(subMenu); 
      } 
      
      public void actionPerformed(ActionEvent e) 
    { 
        String item = e.getActionCommand(); 
        if (item.equals("First")) 
          System.out.println("First"); 
        else if (item.equals("Help"))  
          System.out.println("Help"); 
      } 
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to create Menus in Java Application?

    Hi, I also yet don't know how to create menus in java, but when I read your post on this forum, I searched it and find one best program to make menu in java. It's so simle, and I think that will help you also.

    Code:
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class check extends JFrame
    {
    	private JMenuBar menu;
    	private JMenu m1,m2,m3;
    	private JMenuItem AddNew,Search,Display,Delete,Exit,Modify,Help;
    	private JButton btnAddNew,btnSearch,btnDelete,btnDisplay,btnModify,btnExit;
    	private JPanel pMain,pSouth,pNorth,pCenter;
    	private JTextArea tac;
    	private JLabel lbllogo;
    
    	public check()
    	{
    	  menu = new JMenuBar();
    	  m1 = new JMenu("Options");
    	  m2 = new JMenu("Programs");
    	  m3 = new JMenu("Help");
    
    	  AddNew = new JMenuItem("AddNew");
    
    	  Search = new JMenuItem("Search");
    
    	  Display = new JMenuItem("Display");
    	  Delete = new JMenuItem("Delete");
    	  Modify = new JMenuItem("Modify");
    	  Exit = new JMenuItem("Exit");
    	  Help = new JMenuItem("Help");
              tac = new JTextArea(2,3);
    
    	  tac.setText("For: techarena ofTechnology"+""+"forum");
    	  tac.setForeground(Color.red);
    	  tac.setEditable(false);
    
    	  //button intialization
    	  btnAddNew = new JButton("ADDNEW");
    
    	  btnAddNew.setToolTipText("Add new Details");
    
    
    	  btnSearch = new JButton("SEARCH");
    	  btnSearch.setToolTipText("Search particular student");
    
    
    	  btnDelete = new JButton("DELETE");
    	  btnDelete.setToolTipText("Delete particular student");
    
    
    	  btnDisplay = new JButton("DISPLAY");
    	  btnDisplay.setToolTipText("Display particular student");
    
    
    	  btnModify = new JButton("MODIFY");
    	  btnModify.setToolTipText("Modifies particular student");
    
    
    	  btnExit = new JButton("EXIT");
    	  btnExit.setToolTipText("Out of Program");
    
    	   pMain = new JPanel();
    	   pNorth = new JPanel();
               pSouth = new JPanel();
               pCenter = new JPanel();
    
           lbllogo = new JLabel(new ImageIcon("//G:/MFCfinish.gif"),JLabel.CENTER);
    
    	   m1.add(AddNew);
    	   m1.add(Search);
    	   m1.add(Display);
    	   m1.add(Delete);
    	   m1.add(Modify);
    
    	   m2.add(Exit);
    	   m3.add(Help);
    
    	   menu.add(m1);
    	   menu.add(m2);
    	   menu.add(m3);
    
    	   pMain.add(btnAddNew);
    	   pMain.add(btnSearch);
    	   pMain.add(btnDelete);
    	   pMain.add(btnDisplay);
    	   pMain.add(btnModify);
    	   pMain.add(btnExit);
    
    	   pMain.setLayout(new BoxLayout(pMain,BoxLayout.Y_AXIS));
    	   pMain.setBorder(BorderFactory.createTitledBorder("OPTIONS"));
    	   pMain.setLayout(new GridLayout(6,1));
    	   pMain.setBackground(Color.white);
    
    	   pCenter.setBackground(Color.red);
    	   pCenter.setLayout(new BoxLayout(pMain,BoxLayout.Y_AXIS));
    	   pCenter.setLayout(new GridLayout(2,1));
    	   pCenter.add(lbllogo);
    	   pCenter.add(tac);
    
    	   pNorth.setBackground(Color.white);
    
    	   pNorth.add(menu);
    
    	   this.getContentPane().add(pMain,"West");
    	   this.getContentPane().add(pCenter,"Center");
    	   this.getContentPane().add(pNorth,"North");
    
    	   this.setSize(400,300);
    	   this.setResizable(false);
    	   this.setLocation(150,150);
    	   this.setTitle("MENU");
    	   this.show();
    	   }
    
    	   public static void main()
    	   {
    	    test lg = new test();
    	   }
    	  }

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to create Menus in Java Application?

    Hi, I have got the code to create menu with the help of css which will help you to create a hover menu. I don't know it in java. Just check whether it is helpful to you or not.

    Code:
    <style type="text/css">
    
    #coolmenu
    {
    border: 2px solid blue;
    width: 200px;
    background-color: #E6E6E6;
    }
    
    #coolmenu a
    {
    font: bold 13px Verdana;
    padding: 2px;
    padding-left: 4px;
    display: block;
    width: 100%;
    color: black;
    text-decoration: none;
    border-bottom: 1px solid black;
    }
    
    html>body #coolmenu a
    {
    width: auto;
    }
    
    #coolmenu a:hover
    {
    background-color: black;
    color: white;
    }
    
    </style>
    
    <div id="coolmenu">
    <a href="http://www.javascriptkit.com">JavaScript Kit</a>
    <a href="http://www.javascriptkit.com/cutpastejava.shtml">Free JavaScripts</a>
    <a href="http://www.javascriptkit.com/jsref/">JavaScript Reference</a>
    <a href="http://www.techarena.in">Technical assistance</a>
    <a href="http://www.sun.com">java forum</a>
    </div>

Similar Threads

  1. How to create Web Browser with the help of JAVA
    By StudyBoy in forum Software Development
    Replies: 7
    Last Post: 15-03-2012, 02:52 AM
  2. Can I create .exe file in java?
    By LostIt in forum Software Development
    Replies: 3
    Last Post: 08-10-2009, 06:23 PM
  3. How to create XML file in Java
    By Suzane in forum Software Development
    Replies: 3
    Last Post: 27-04-2009, 06:00 PM
  4. Create Popup Menus without a Form in VB
    By Ariadne in forum Software Development
    Replies: 2
    Last Post: 26-01-2009, 06:59 PM
  5. Create a Java application
    By C.M.D in forum Software Development
    Replies: 3
    Last Post: 22-10-2008, 05:01 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,520,588.25719 seconds with 16 queries