Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to create Menus in Java Application?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 30-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
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.

Reply With Quote
  #2  
Old 30-11-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
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"); 
  } 
}
Reply With Quote
  #3  
Old 30-11-2009
Member
 
Join Date: May 2008
Posts: 2,008
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();
	   }
	  }
Reply With Quote
  #4  
Old 30-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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>
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 06:54 AM.