Solved

key listener for menuitem

Posted on 2001-09-05
7
440 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

710 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