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
Solved

key listener for menuitem

Posted on 2001-09-05
7
438 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle 11g 23 106
String array comparison 4 38
Chrome and Firefox Java 5 50
ejb mdb examples 1 6
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

808 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