Solved

key listener for menuitem

Posted on 2001-09-05
7
439 Views
Last Modified: 2006-11-17
Helo
we have developed one application and in that application we hava status bar like the browser .. whatever we select the message text will be displayed in the status bar..
actually for the jmenuitem r menuitem iam able to add mouse motion listener,but not able to do with key listeners r key pressed events..
if anybody have done it pl explain with the code
0
Comment
Question by:aravindmalai
7 Comments
 
LVL 9

Accepted Solution

by:
yongsing earned 25 total points
ID: 6459735
You can add a ChangeListener to your JMenuItem. When the menu item is selected, you check whether it is armed. If it is, then you can display the message associated with this menu item. Otherwise, you clear away the existing message on the status bar.

I use the setAccessibleDescription() method to associate a message with each JMenuItem. When a particular JMenuItem is selected, the assessible description is retrieved and displayed.

For the status bar, I use a JLabel which is contained in a JPanel. The message is set on the status bar by calling the setText() method.

Below is an example which shows the creation of an "Open..." JMenuItem. When the item is selected, the message "Open a file" is displayed on the status bar.

JMenuItem openItem = new JMenuItem("Open...", KeyEvent.VK_O);
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
openItem.getAccessibleContext().setAccessibleDescription("Open a file");
openItem.addActionListener(this);
openItem.addChangeListener(
    new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            JMenuItem menuItem = (JMenuItem)changeEvent.getSource();
            if (menuItem.isArmed()) {
                statusLine.setText(menuItem.getAccessibleContext().getAccessibleDescription());
            } else {
                statusLine.setText(null);
            }
        }
    }
);
getContentPane().add(openItem);
0
 
LVL 6

Expert Comment

by:kotan
ID: 6459739
You should use ActionListener.

jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
               
            }
});
0
 
LVL 6

Expert Comment

by:kotan
ID: 6459757
keylistener doesn't work in menuitem.

There is menukeylistener which listen to the keyboard press on the menu.

ActionListener is trigger when the menu is selected.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:aravindmalai
ID: 6459762
hi guys
what i need is that when i select the menu item with the key the corresponding menuitem text should be displayed in the staus bar
 attmempted with menu key pressed and with menu jeu released and with menu selected and men deselected butits attempting to get the menu only not with menuitem ..i aslo
give the sample code where we have dome with
mouse listeners ....
*******************************************************
code for mouse listener
**********************************
public void mouseMoved(MouseEvent me)
     {
         Object oSrc = me.getSource();
         ESMCommand cmd = null;

         if (oSrc instanceof ESMMenuItem)
             cmd = ((ESMMenuItem)oSrc).getCommand();
         else if (oSrc instanceof ESMToolBarButton)
             cmd = ((ESMToolBarButton)oSrc).getCommand();
         else if (oSrc instanceof ESMCheckMenuItem)
             cmd = ((ESMCheckMenuItem)oSrc).getCommand();

          if (cmd != null)
          {
              ESMStatusBar statbar = ESMConsole.getConsole().getMainFrame().getStatusBar();
              ESMStatusText statusText = statbar.getMessageText();
              statusText.setValue(cmd.getLongHelp());
          }
     }

*****************************************
this i  have to perform with key listener
0
 
LVL 9

Expert Comment

by:yongsing
ID: 6459776
Use ChangeListener as I suggested above.

public void stateChanged(ChangeEvent changeEvent) {
    Object object = changeEvent.getSource();
    if (object instanceof JMenuItem) {
        JMenuItem menuItem = (JMenuItem)object;
        if (menuItem.isArmed()) {
            statusBar.setText(menuItem.getText());
        } else {
            statusBar.setText(null);
        }
    }
}
0
 
LVL 20

Expert Comment

by:Venabili
ID: 8908424
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- points for yongsing

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 43
sql import cannot be resolved jsp 3 72
Bot application - advice 3 58
Java class and jar 3 41
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question