Solved

key listener for menuitem

Posted on 2001-09-05
7
431 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now