Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2007

Tip: Looking for answers? Try searching our database.

javax.swing.JPopupMenu cannot be cast to javax.swing.JMenuItem

Thread view: 
dushkin - 18 Apr 2007 14:56 GMT
Hello All,

To get the text of runtime built menu items, I used the following
code:
-------------------------------------------------------------------------------------------------------

    //Create a change listener and register with the menu selection
manager
       MenuSelectionManager.defaultManager().addChangeListener(
           new ChangeListener() {
               public void stateChanged(ChangeEvent evt) {
                   // Get the selected menu or menu item
                   MenuSelectionManager msm =
(MenuSelectionManager)evt.getSource();
                   MenuElement[] path = msm.getSelectedPath();
                   if (path != null && path.length > 0){
              ------------>      String m_sSelectedMenuItem =
((JMenuItem)(path[path.length - 1])).getText();
                   }
               }
           }
       );
-------------------------------------------------------------------------------------------------------
However, I recieve the following exception
    java.lang.ClassCastException: javax.swing.JPopupMenu cannot be
cast to javax.swing.JMenuItem
on the stared pointed line above.
This is obviously also hardens my debugging work as the exception
always being thrown when I select the menu item...

Any ideas?
Many thanks!
Andrew Thompson - 18 Apr 2007 15:07 GMT
..
>     java.lang.ClassCastException: javax.swing.JPopupMenu cannot be
>cast to javax.swing.JMenuItem
..
>Any ideas?

1) instanceof
2)
try {
 ..one
catch(CCE) {
 try {
  ..the other
 catch(CCE) {
  ..debug the code
 }
}
3) redesign it

Signature

Andrew Thompson
http://www.athompson.info/andrew/

dushkin - 18 Apr 2007 16:09 GMT
First andrew thanks for the reply.
Second, It may be the late afternoon which causes me a little to
misunderstand your suggestions... I'll be glad to get more detailed
help. I really didn't understand the solution...
dushkin - 18 Apr 2007 17:04 GMT
dushkin כתב:
> First andrew thanks for the reply.
> Second, It may be the late afternoon which causes me a little to
> misunderstand your suggestions... I'll be glad to get more detailed
> help. I really didn't understand the solution...

Ok, I got it with the instanceof....
Thanks again!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.