menuItem and actionPerformed

Ads

 
 

Share on Google+Share on Google+

syed firoze
menuItem and actionPerformed
2 Answer(s)      7 years 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


Ads









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
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...;new JMenuItem("Cut");   Pmenu.add(menuItem);   ...(menuItem);   menuItem = new JMenuItem("Paste"
FlexNativeMenu Control in Flex4
;{menuItem}" labelField="@label" keyEquivalentField="...;menuitem label="File"> <menuitem label="New" keyEquivalent="n" controlKey="true"/> <menuitem label="
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
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
;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
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
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
;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
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

Ads

 
Advertisement null

Ads