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 / January 2004

Tip: Looking for answers? Try searching our database.

MenuItem actionListener

Thread view: 
- ions - 15 Jan 2004 04:31 GMT
Hi, im having trouble getting an actionListener to work on a
JMenuItem.. this is the method..

public JMenuItem createMenuItem(JMenuItem item, String ItemName,
                                               boolean isEnabled)
{
  item = new JMenuItem(ItemName);
  item.addActionListener(this); //Uneffective!

  item.setEnabled(isEnabled);

  return item;
}

then add it to the JMenu/PopupMenu..

private JMenuItem newItem;
 ...
 ...
fileMenu.add(createMenuItem(newItem,"New",true));

then the actionPerformed()

actionPerformed(ActionEvent ae)
{
  if(ae.getSource() == newItem)
  {
     //action code
  }
}

The MenuItem is created, the String is passed to the label as the
MenuItem's name and the enabled state is set, but the actionListener
is uneffective?
hiwa - 15 Jan 2004 12:44 GMT
>    if(ae.getSource() == newItem)
>    {
>       //action code
>    }
> }
Is it really newItem? Try using getText() on the event source.
And/or add ActionListener directly to the newItem.
Peter Votruba - 15 Jan 2004 16:27 GMT
> Hi, im having trouble getting an actionListener to work on a
> JMenuItem.. this is the method..

...

> The MenuItem is created, the String is passed to the label as the
> MenuItem's name and the enabled state is set, but the actionListener
> is uneffective?

newItem will always be "null", because createMenuItem will not change, and
therefore "(ae.getSource() == newItem)" will never evaluate to true.
Try this instead:

public JMenuItem createMenuItem(String ItemName, boolean isEnabled)
{
  JMenuItem item = new JMenuItem(ItemName);
  item.addActionListener(this); //Uneffective!

  item.setEnabled(isEnabled);

  return item;
}
...
private JMenuItem newItem1;
private JMenuItem newItem2;
 ...
 ...
newItem1 = createMenuItem("New",true);
fileMenu.add(newItem1);
newItem2 = createMenuItem("Exit",true);
fileMenu.add(newItem2);
...
... actionPerformed(ActionEvent ae)
{
  if(ae.getSource() == newItem1) {
     //action code
  } else if(ae.getSource() == newItem2) {
     //action code
  }
}

But much better would be, to use Actions for each MenuItem.

HTH, Peter
Gregory A. Swarthout - 15 Jan 2004 17:48 GMT
> Hi, im having trouble getting an actionListener to work on a
> JMenuItem.. this is the method..
[quoted text clipped - 30 lines]
> MenuItem's name and the enabled state is set, but the actionListener
> is uneffective?

That's because you've added "new JMenuItem(ItemName)" to your menu,
not "newItem".  You passed in a reference to "newItem" but the method
does not use that reference, it discards it in favor of a new
JMenuItem.  This DOES NOT assign the new JMenuItem to "newItem".

Greg


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



©2009 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.