Results 1 to 8 of 8

Thread: Need some info on menus

  1. #1

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183

    Need some info on menus

    I am interested in learning how to do drop down menus in my Java GUI's. I just need some information before i get started

    What classes are needing for drop down menus? How does Java respond to a menu selection? Does it use a ListSelectionListener or an ActionEventListener?

    Can I see a very simple example of a drop down menu.
    Last edited by ddmeightball; Mar 4th, 2005 at 12:23 PM.
    Otaku no Kamisama – God of the Geeks

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Need some info on menus

    If you use Swing, which is what I would recommend, use JComboBox:
    Code:
    JComboBox jcb = new JComboBox();
    jcb.addActionListener(this);
    
    
    then to add stuff to it:
    
    jcb.addItem("One");
    jcb.addItem("Two");
    
    
    in the actionPeformed method:
    I would do this:
    
    	String strWhich = (String)jcb.getSelectedItem();
    
    if (strWhich  == "One")
    {
       //do something
    }
    I do have an example from a while back that converts currency, let me know if your interested in seeing it.

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Need some info on menus

    Do you mean a combo box or a menu? Here is an example if a menu is what you want :

    Code:
    import java.awt.*;
    import java.awt.event.*;
    
    public class TestMenu extends Frame {//implements ActionListener{
    
    	public static void main(String args[]) {
    		TestMenu T=new TestMenu("Window title");
    		T.pack();
    		T.show();
    	}
    
    	public TestMenu(String title) {
    		super(title);
    
    		setLayout(new BorderLayout());
    
    		Button bAdd=new Button("Button");
    		add(bAdd);
    
    		FileMenu fileMenu = new FileMenu(this);
    
    		MenuBar mb = new MenuBar();
    		mb.add(fileMenu);
    		setMenuBar(mb);
    
    		addWindowListener(new WindowAdapter(){
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		});
    	}
    
        public void exit() {
    		setVisible(false);
    		dispose();
    		System.exit(0);
    	}
    
    }
    
    class FileMenu extends Menu implements ActionListener {
      TestMenu TM;  // who owns us?
      public FileMenu(TestMenu tm) {
        super("File");
        TM = tm;
        MenuItem mi;
        add(mi = new MenuItem("Exit"));
        mi.addActionListener(this);
      }
    
      public void actionPerformed(ActionEvent e) {
        String item = e.getActionCommand();
        if (item.equals("Exit")) {
    		TM.exit();
        } else {
          System.out.println("Selected FileMenu " + item);
    	}
      }
    }


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Need some info on menus

    Maybe that's what he meant!

  5. #5
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Need some info on menus

    Just for addition to Phillip's post. You can do it like this on swing
    VB Code:
    1. import java.awt.*;
    2. import java.awt.event.*;
    3. import javax.swing.*;
    4.  
    5. public class test extends JFrame{
    6.     JMenuBar mbar=new JMenuBar();
    7.     JMenu mnufile=new JMenu("File");
    8.     JMenuItem mnuexit=new JMenuItem("Exit");
    9.     public test(){
    10.         setDefaultCloseOperation(EXIT_ON_CLOSE);
    11.         setSize(300,300);
    12.  
    13.         setJMenuBar(mbar);
    14.         mbar.add(mnufile);
    15.         mnufile.add(mnuexit);
    16.  
    17.         mnuexit.addActionListener(new ActionListener(){
    18.             public void actionPerformed(ActionEvent e){
    19.                 mnuexit_actionPerformed(e);
    20.             }
    21.         });
    22.     }
    23.     void mnuexit_actionPerformed(ActionEvent e){
    24.         System.exit(0);
    25.     }
    26.  
    27.     public static void main(String[] args){
    28.         new test().setVisible(true);
    29.     }
    30. }
    Btw, show is deprecated, so refrain from using it. Also I created the mnuexit_actionPerformed on the class' scope. This is just for some use. You can access it from within the class.

  6. #6

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183

    Re: Need some info on menus

    I wanted a menu, not a combo box. Kinda like the menu that we all have in our internet browsers. you know, FILE, EDIT, VIEW, etc.
    Otaku no Kamisama – God of the Geeks

  7. #7

    Thread Starter
    Addicted Member ddmeightball's Avatar
    Join Date
    Nov 2004
    Location
    Nebraska
    Posts
    183

    Re: Need some info on menus

    Ok, i was tinkering around with this and I wanted to just make an ActionPerformed method, not an abstract method. The problem comes up when i try to add the actionlistener to my exit JMenuItem.

    mnuexit.addActionListener(this);

    I get this error
    addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (TestMenu2)
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class TestMenu2 extends JFrame{
    	JMenuBar mbar=new JMenuBar();//creates the menu bar
    	
    	JMenu mnufile=new JMenu("File");//creates the file selection of the menu bar
    	JMenuItem mnuexit=new JMenuItem("Exit");//creates the options for the file menu
    
    	JMenu mnuEdit = new JMenu("Edit");//create the edit selection of the menu bar
    	JMenuItem mnuCopy=new JMenuItem("Copy");//creates the options for the file menu
    
    	
    	public TestMenu2(){
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setSize(300,300);
    
    		setJMenuBar(mbar);
    		mbar.add(mnufile);//adds "File"
    		mnufile.add(mnuexit);//adds "Exit" option for the file selection
    		
    		mbar.add(mnuEdit);//adds "Edit"
    		mnuEdit.add(mnuCopy);//adds copy to Edit
    
    		mnuexit.addActionListener(this);
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		if(e.getSource()=="Exit"){
    			System.exit(0);		
    		}
    	}
    
    	public static void main(String[] args){
    		new TestMenu2().setVisible(true);
    	}
    }
    Otaku no Kamisama – God of the Geeks

  8. #8
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Need some info on menus

    you didn't implement the actionListener?

Posting Permissions

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



Click Here to Expand Forum to Full Width