menuItem and actionPerformed

menuItem and actionPerformed

i tried to do notepad application. but in that,when i clicks CUT menuItem of EDIT menu,i wanna get the MENU label("edit") inside the actionPerformed.how could i get it

View Answers

October 23, 2010 at 11:06 AM

Hi Friend,

Try the following code:

import java.awt.*; import javax.swing.*; import java.awt.event.*;

public class MenuExample extends JFrame { JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("File"); JMenu menu1 = new JMenu("Edit"); JMenuItem item1 = new JMenuItem("New"); JMenuItem item2 = new JMenuItem("Open");

public MenuExample(){ setJMenuBar(menuBar); setVisible(true); setSize(400,200); menuBar.add(menu); menuBar.add(menu1); menu.add(item1); menu.add(item2); item1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser chooser=new JFileChooser(); chooser.showOpenDialog(null);

        }

    });
     item2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser=new JFileChooser();
            chooser.showOpenDialog(null);

        }

    });

} public static void main(String[]args){ MenuExample ex=new MenuExample(); }

}

Read more at:

http://www.roseindia.net/java/example/java/swing/

Thanks


October 23, 2010 at 11:08 AM

Hi Friend,

Try the following code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MenuExample extends JFrame
{
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenu menu1 = new JMenu("Edit");
JMenuItem item1 = new JMenuItem("New");
JMenuItem item2 = new JMenuItem("Open");

public MenuExample(){
setJMenuBar(menuBar);
setVisible(true);
setSize(400,200);
menuBar.add(menu);
menuBar.add(menu1);
menu.add(item1);
menu.add(item2);
item1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser=new JFileChooser();
                chooser.showOpenDialog(null);

            }

        });
         item2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser=new JFileChooser();
                chooser.showOpenDialog(null);

            }

        });
}
public static void main(String[]args){
    MenuExample ex=new MenuExample();
}

}

Read more at:

http://www.roseindia.net/java/example/java/swing/

Thanks









Related Tutorials/Questions & Answers:
menuItem and actionPerformed
menuItem and actionPerformed  i tried to do notepad application. but in that,when i clicks CUT menuItem of EDIT menu,i wanna get the MENU label("edit... actionPerformed(ActionEvent e) { JFileChooser chooser=new JFileChooser
Dojo: How MenuItem helps page redirction - Ajax
Dojo: How MenuItem helps page redirction  Hi, I am wondering that if "MenuItem" accepts an event of window.location (page redirection) like this : Response : NO, stay at same page. Is there any simple way
Advertisements
Pop-up Menus
; A PopupMenu is similar to a Menu as it contains MenuItem objects. The Pop...; public void actionPerformed(ActionEvent e){   ...;    add(mi = new MenuItem("Copy")); 
Menu Shortcuts
; The MenuItem class also provides a feature of menu shortcuts and speed keys... of the MenuItem can be created with the help of MenuItem class. The use of Ctrl... and Shift-Ctrl-P for Print Preview:ADS_TO_REPLACE_1 file.add (m = new MenuItem
Create Menu Bar in SWT
, the classes Menu and MenuItem of package org.eclipse.swt.widgets is allowed...;(((MenuItem) event.widget).getText().equals("Exit")) {  ...);   MenuItem fileItem = new MenuItem(menu, 
Menus
of Menu objects. These Menu objects comprise of MenuItem objects which can be selected by the user with a click of a mouse. A MenuItem may be a String, checkbox...;  3.  Now the MenuItem options can be added to the Menu from top
Java controls
Java controls  What is the difference between a MenuItem and a CheckboxMenuItem
JFRAME - Java Beginners
menuItems(new,open).. actionevent given to open(menuitem)... now when click on open...(new java.awt.event.ActionListener() { public void actionPerformed...() { public void actionPerformed(ActionEvent e) { JFileChooser
Crate a Popup Menu in Java
;JMenuItem menuItem;   public static void main(String[] ...;JMenuItem("Cut");   Pmenu.add(menuItem);   menuItem = new JMenuItem("Copy");   Pmenu.add(menuItem);  
FlexNativeMenu Control in Flex4
:FlexNativeMenu dataProvider="{menuItem}" labelField="@label"... <fx:XML id="menuItem"> <root> <menuitem label="File"> ADS_TO_REPLACE_5 <menuitem label="New"
retaining save dialog box even after clicking save button - Java Beginners
textArea=new JTextArea("",5,40); static JMenuItem menuItem; public static void..."); menu.setMnemonic(KeyEvent.VK_F); menuBar.add(menu); menuItem... Listener()); menu.add(menuItem); JScrollPane scrollPane = new
Applet - Applet
"); m.add(new MenuItem("Shortcut Keys")); m.add(new MenuItem("Version")); mb.add(m...); } public void paint(Graphics g) { if(evt.target instanceof MenuItem... actionPerformed(ActionEvent e) { String text="Home=Move to start of the current
JTable
JTable  i want to delete record from JTable using a MenuItem DELETE. and values of JTable are fetched from database....please reply soon
MenuBar Control in Flex4
;menuitem label="File" icon="topIcon"> <menuitem label="New" /> <menuitem label="Open" /> ADS_TO_REPLACE_9 <menuitem label="Save" icon="top1Icon"/> <
Hash Table for maintaining popup menus in Java
;= new MenuItem("First Menu");   ...;new MenuItem("Second Menu");   m.add(item);   ...; public void actionPerformed(ActionEvent e){   
Menu Control in Flex4
;   var menu1:Menu= Menu.createMenu(null, menuItem, false... = Menu.createMenu(null, menuItem, false);ADS_TO_REPLACE_8 menu1.labelField...;root> ADS_TO_REPLACE_11 <menuitem label="MenuItem A"> <
java script - Development process
) { menuItem="document.layers"; styleSwitch... if(document.all){ menuItem="document.all... if(document.getElementById){ menuItem="document.getElementByID
Computers - Java Beginners
CalcFrame fr; MenuBar menu; Menu view, font; MenuItem bold, regular... = new MenuItem("Arial Bold"); bold.addActionListener(this); regular = new MenuItem("Arial Regular"); regular.addActionListener
java code
; MenuItem student,rollnowise,namewise,allresult; public static menu m; rollnowise...(this); }ADS_TO_REPLACE_3 public void actionPerformed(ActionEvent ae..."); dlg.add(msg); dlg.add(ok); } public void actionPerformed(ActionEvent
i am getting an error in adding a menu item to a menu.
i am getting an error in adding a menu item to a menu.   i am not able to execute the menu program. i am getting an error at menu.add(menuitem); it says add method is undefined for type menu. but as far as i know that is only way
Question?
is to shown on menuitem selection. import java.awt.*; import java.util....() { public void actionPerformed(ActionEvent e
PopUpMenuButton Control in Flex4
; <menuitem label="New"/> <menuitem label="Open"/> ADS_TO_REPLACE_7 <menuitem label="Save"/> <menuitem label="Save as..."/> <menuitem label="Close"
Events
; TextListener  Button  List  MenuItem  ...;windowDeactivated(WindowEvent e)  ActionListener  actionPerformed
ArrayList - Java Beginners
() { public void actionPerformed(ActionEvent ae) { btnAdd.doClick... void actionPerformed(ActionEvent ae) { vector.add(txtAdd.getText...(); } }); btnSize.addActionListener(new ActionListener() { public void actionPerformed
GUI problem - Java Beginners
; Handle the actionPerformed event for JButton and try doing something like...; Handle the actionPerformed event for JButton and try doing something like
Core Java Interview Question, Interview Question
: What is the difference between a MenuItem and a CheckboxMenuItem? Answer: The CheckboxMenuItem class extends the MenuItem class to support a menu item... is the immediate superclass of Menu? Answer: MenuItem ADS_TO_REPLACE_11 Question
java button - Java Beginners
ActionListener() { public void actionPerformed(ActionEvent e...() { public void actionPerformed(ActionEvent e) { trace("2", cb2...() { public void actionPerformed(ActionEvent e) { trace("3", cb3
Java Evet Erropr - Java Beginners
: actiondemo is not abstract and does not override abstrac thod actionPerformed...); b.addActionListner(this); } public void ActionPerformed(ActionEvent ae...(){ public void actionPerformed(ActionEvent ae){ b.setLabel("Button Clicked
calender applet
)); ddText.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e...())); } }); add(p, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { System.out.println("Cal::ActionPerformed"); yy = Integer.parseInt
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
) { ................... ON.addActionListener(new ActionListener() { public void actionPerformed...() { public void actionPerformed(ActionEvent ev) { t.stop... actionPerformed(ActionEvent ae) { String value1 = text1.getText
NotePad - Swing AWT
() { public void actionPerformed(ActionEvent e... actionPerformed(ActionEvent e) { String str=newODialog... ActionListener() //save dialog { public void actionPerformed
Java Code - Java Beginners
); item1.addActionListener(new ActionListener(){ public void actionPerformed... ActionListener(){ public void actionPerformed(ActionEvent e){ JLabel label2=new... actionPerformed(ActionEvent e){ JLabel label3=new JLabel("This is Panel3
help me
(){ public void actionPerformed(ActionEvent e){ String st=text.getText...(new ActionListener(){ public void actionPerformed(ActionEvent e... actionPerformed(ActionEvent e){ String st=text.getText(); String reverse
java
not override the abstract method actionPerformed. For implementing an interface
java
); } public void ActionPerformed(ActionEvent e) { t.setText(e.getActionCommand
Swings - Java Beginners
ActionListener() { public void actionPerformed(ActionEvent e...); } }); jTextField2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent... actionPerformed(ActionEvent e) { jTextField3_actionPerformed(e
Swings - Java Beginners
ActionListener() { public void actionPerformed(ActionEvent e...); } }); jTextField2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent... actionPerformed(ActionEvent e) { jTextField3_actionPerformed(e
Average
actionPerformed(java.awt.event.ActionEvent evt) { txtStudentIDActionPerformed...() { public void actionPerformed(java.awt.event.ActionEvent evt...); } }); cboxGrades.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed
connection database error
public void actionPerformed(ActionEvent e) { clear_actionPerformed(e); } } private class ExitActionListener implements ActionListener { // action for button exit public void actionPerformed
swings - Java Beginners
() { public void actionPerformed(ActionEvent e...); } }); jTextField2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e... actionPerformed(ActionEvent e) { jTextField3_actionPerformed(e
swings - Java Beginners
() { public void actionPerformed(ActionEvent e...); } }); jTextField2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e... actionPerformed(ActionEvent e) { jTextField3_actionPerformed(e
java - Java Server Faces Questions
"); ADD.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae...; } } }); SUB.addActionListener(new ActionListener() { public void actionPerformed...; } } }); MUL.addActionListener(new ActionListener() { public void actionPerformed
validating text fields - Swing AWT
java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt...() { public void actionPerformed(java.awt.event.ActionEvent evt... java.awt.event.ActionListener() { public void actionPerformed
please give me a java program for the following question
ActionListener(){ public void actionPerformed(ActionEvent e){ JFrame f1=new JFrame...); } }); button2.addActionListener(new ActionListener(){ public void actionPerformed... ActionListener(){ public void actionPerformed(ActionEvent e){ JFrame f1=new
Java Web Browser Problem
actionPerformed(ActionEvent e) { try... actionPerformed(ActionEvent event) { if(urlStack.size()<=1...() { public void actionPerformed(ActionEvent event
swings - Java Beginners
); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e... actionPerformed(ActionEvent e){ f.setVisible(false
java code - Swing AWT
ActionListener(){ public void actionPerformed(ActionEvent e... ActionListener(){ public void actionPerformed(ActionEvent e
JTable - Cell selection - Swing AWT
actionPerformed(ActionEvent e){ int row = table.getSelectedRow(); int column... actionPerformed(ActionEvent e) { if (table.isEditing
how to upload photo - Java Beginners
void actionPerformed(ActionEvent ae){ int returnval = fc.showOpenDialog(null... actionPerformed(ActionEvent ae){ // Add functionality for inserting image
java buttons - Swing AWT
actionPerformed(ActionEvent e) { p1.setVisible(false...() { public void actionPerformed(ActionEvent e

Ads