hi...
i am new to this community, lately i was looking for some help in my code and this website came to my rescue in many topic.
i am just implementing the menubar in swing, can somebody help me in my code.

import java.lang.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.util.*;

public class FrameExample extends JFrame {
    
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenu optionMenu;
    private JMenuItem newFileItem;
    private JMenuItem openFileItem;
    private JMenuItem exitItem;
     
    public FrameExample()
    {
    	menuBar = new JMenuBar();
    	setJMenuBar(menuBar);
    	MyMenuHandler handler = new MyMenuHandler();
    	
    	newFileItem=new JMenuItem();
    	newFileItem.setText("New");
    	newFileItem.setToolTipText("Click to open new file");
    	newFileItem.setActionCommand("new");
    	newFileItem.addActionListener(handler(this));
    	
    	openFileItem=new JMenuItem();
    	openFileItem.setText("Open");
    	openFileItem.setToolTipText("Click to open existing file");
    	openFileItem.setActionCommand("open");
    	openFileItem.addActionListener(handler(this));
    	
    	exitItem=new JMenuItem();
    	exitItem.setText("Exit");
    	exitItem.setToolTipText("Click to Exit");
    	exitItem.setActionCommand("exit");
    	exitItem.addActionListener(handler(this));
    	
    	fileMenu=new JMenu();
    	fileMenu.add(newFileItem);
    	fileMenu.add(openFileItem);
    	fileMenu.add(exitItem);
    	
    	addWindowListener(new MyWindowAdapter());
    	setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    }
    
    public static void main(String[] args)
    {
    	FrameExample frame=new FrameExample();
    	frame.setSize(200,200);
    	frame.setTitle("A Frame");
    	frame.setVisible(true);
    
    }
    
     class MyMenuHandler extends ActionListener{
    	FrameExample f;
    	
    	
    	public void actionPerformed(ActionEvent ae)
    	{
    		String arg=(String)ae.getActionCommand();
    		
    		if(arg.equals("new"))
    		{
    			JOptionPane.showMessageDialog(FrameExample.this, "New File Chosen", JOptionPane.CANCEL_OPTION);
    		}
    	}
    	
    }
}
   class MyWindowAdapter extends WindowAdapter{
    	
    	public void windowClosing(WindowEvent we)
    	{
    		int result = JOptionPane.showConfirmDialog( FrameExample.this, "Do you really Want to exit?", "Confirmation", JOptionPane.YES_NO_OPTION );
               if( result == JOptionPane.YES_OPTION )
               {
                  System.exit( 0 );
               }
    	}
    
    }

Recommended Answers

All 3 Replies

Here is working sample based on code provided by you

import java.lang.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.util.*;

public class FrameExample{
    
    private JFrame frame;
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenu optionMenu;
    private JMenuItem newFileItem;
    private JMenuItem openFileItem;
    private JMenuItem exitItem;
     
    public FrameExample()
    {
    	frame = new JFrame();
    	menuBar = new JMenuBar();
    	frame.setJMenuBar(menuBar);
    	
    	newFileItem=new JMenuItem();
    	newFileItem.setText("New");
    	newFileItem.setToolTipText("Click to open new file");
    	newFileItem.setActionCommand("new");
    	newFileItem.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent ae){
    			JOptionPane.showMessageDialog(frame, "New File Chosen","New File", JOptionPane.CANCEL_OPTION);
    			/*Be carefull with declaration you used only 3 arguments with this method 
    			 *where there is option only for 2, 4, 5 and 8 arguments.
    			 */
    		}
    	});
    	
    	openFileItem=new JMenuItem();
    	openFileItem.setText("Open");
    	openFileItem.setToolTipText("Click to open existing file");
    	openFileItem.setActionCommand("open");
    	openFileItem.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent ae){
    			//action here
    		}
    	});
    	
    	exitItem=new JMenuItem();
    	exitItem.setText("Exit");
    	exitItem.setToolTipText("Click to Exit");
    	exitItem.setActionCommand("exit");
    	exitItem.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent ae){
    			//action here
    		}
    	});
    	
    	fileMenu=new JMenu("File");//missing menu name it would be not visible even if added to JMenuBar
    	fileMenu.add(newFileItem);
    	fileMenu.add(openFileItem);
    	fileMenu.add(exitItem);
    	menuBar.add(fileMenu);//never added to JMenuBar
    	frame.setJMenuBar(menuBar);//JMenuBar never added to frame
    	
    	frame.addWindowListener(new WindowAdapter(){
    		public void windowClosing(WindowEvent we){
    		int result = JOptionPane.showConfirmDialog( frame, "Do you really Want to exit?", "Confirmation", JOptionPane.YES_NO_OPTION );
               if( result == JOptionPane.YES_OPTION )
               {
                  System.exit( 0 );
               }
    	}
    	});
    	frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    	frame.setSize(400,400);
    	frame.setTitle("A Frame");
    	frame.setVisible(true);
    }
    
    public static void main(String[] args)
    {
    	SwingUtilities.invokeLater(new Runnable(){
    		public void run(){
    			new FrameExample();
    		}
    	});
    }
}
commented: helpful +4

thanks peter_budo :)

yup its working now, but can you tell whats the problem in creating a new handler(MyMenuHandler in my code) to handle Action Event of menu items.

openFileItem.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent ae){
    			//action here
    		}
    	});

this kinda code lookss little clumsy.
is there any way getting around this??

There are two ways to handle generated events:
A) You will create single general actionPerformed() method which can be encapsulated in event handler as you tried and then with you of loop and if statement try to figure out which event was triggered. This approach makes your code short as you do not have to repeat some lines over and over. Unfortunately with number of events attached to this approach you are running risk of making mistake in event recognition and spending long time checking out if the logic inside is working as you expect.

B) Do it the way as showed in my code by providing actionPerformed() to every object that need it. By doing so you code will be longer as some code will be repeated. However when some action doesn't work as you intended you just go to specific section of code and check out "what the hell is going on!"

Consider what is better for you, as you can see I prefer option B for its easy debugging.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.