Share on Google+Share on Google+

syed firoze
menuItem and actionPerformed
2 Answer(s)      6 years and 6 months ago
Posted in : Java Beginners

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

Ads

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...); item1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent
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){    m.show(b...; 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: file.add (m = new MenuItem ("Print", new
Create Menu Bar in SWT
, the classes Menu and MenuItem of package org.eclipse.swt.widgets is allowed... the shell on clicking the exit item: if (((MenuItem) event.widget...;fileItem = new MenuItem(menu, SWT.CASCADE);   
Java controls
Java controls  What is the difference between a MenuItem and a CheckboxMenuItem
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...; Now the MenuItem options can be added to the Menu from top to bottom, using
FlexNativeMenu Control in Flex4
;{menuItem}" labelField="@label" keyEquivalentField="...;menuitem label="File"> <menuitem label="New" keyEquivalent="n" controlKey="true"/> <menuitem label="
Crate a Popup Menu in Java
; JMenuItem menuItem;   public static void main...;new JMenuItem("Cut");   Pmenu.add(menuItem);   ...(menuItem);   menuItem = new JMenuItem("Paste"
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
Menu Control in Flex4
;   var menu1:Menu= Menu.createMenu(null, menuItem, false..., menuItem, false); menu1.labelField="@label"; menu1.iconField="...;root> <menuitem label="MenuItem A"> <menuitem label
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
JTable
JTable  i want to delete record from JTable using a MenuItem DELETE. and values of JTable are fetched from database....please reply soon
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
MenuBar Control in Flex4
;menuBarCollection"> <menuitem label="File" icon="topIcon"> <menuitem label="New" /> <menuitem label="Open" /> <menuitem label="Save" icon="top1Icon
Hash Table for maintaining popup menus in Java
;MenuItem("First Menu");   item.addActionListener(this);   m.add(item);   item = new MenuItem("...;void actionPerformed(ActionEvent e){   
java script - Development process
) { menuItem="document.layers"; styleSwitch... if(document.all){ menuItem="document.all... if(document.getElementById){ menuItem="document.getElementByID
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
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... actionPerformed(ActionEvent ae) { String arg=ae.getActionCommand..."); dlg.add(msg); dlg.add(ok); } public void actionPerformed(ActionEvent
Question?
is to shown on menuitem selection. import java.awt.*; import java.util....() { public void actionPerformed(ActionEvent e
PopUpMenuButton Control in Flex4
;e4x" id="menuData"> <root> <menuitem label="New"/> <menuitem label="Open"/> <menuitem label="Save"/> <menuitem label="Save as..."
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 Question: What is the purpose
Events
;TextField   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
Dojo Menu and Menu Item
then popup menu is appeared on the screen. MenuItem widgets are an actual items in the menu. The PopupMenuItem is a MenuItem, but it displays a submenu or other
Dojo Menu and Menu Item
then popup menu is appeared on the screen. MenuItem widgets are an actual items in the menu. The PopupMenuItem is a MenuItem, but it displays a submenu or other
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
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
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
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads