Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

key listener for menuitem

Posted on 2001-09-05
7
Medium Priority
?
446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 9

Accepted Solution

by:
yongsing earned 100 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
Technology Partners: 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

Technology Partners: 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!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

722 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