I think I messed up my response from the other day. You can do this
they way Victor is talking about. Or you can use the setActionCommand
() method available to most buttons in Swing as follows. I've used
both in practice.

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

public class ActionCommand {

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {

        // Create one action listener for all the menu items. Search
for the source
        // of the action using the getActionCommand() method of the
event.
        ActionListener actionListener = new ActionListener() {

          public void actionPerformed(ActionEvent e) {
            String actionCommand = e.getActionCommand();
            if (actionCommand.equals("item1")) {
              System.out.println("Item 1 was pressed.");
            } else if (actionCommand.equals("item2")) {
              System.out.println("Item 2 was pressed.");
            }
          }
        };

        // Create a menu item with an action command
        JMenuItem item1 = new JMenuItem("Item 1");
        item1.setActionCommand("item1");
        item1.addActionListener(actionListener);

        // Create another menu item with a different action command
        JMenuItem item2 = new JMenuItem("Item 2");
        item2.setActionCommand("item2");
        item2.addActionListener(actionListener);

        JMenu menu = new JMenu("Menu");
        menu.add(item1);
        menu.add(item2);

        JMenuBar menuBar = new JMenuBar();
        menuBar.add(menu);

        JFrame frame = new JFrame("Actions");
        frame.setJMenuBar(menuBar);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setVisible(true);
      }
    });
  }

}


On Sep 24, 11:50 am, Victor Lutin <umg.victor.lu...@gmail.com> wrote:
> Ok, let's see ...
>
> 1. add ActionListener to your menuitem.
> 2. when MenuItem fire the event, you can compare what MenuItem was clicked
>
>      if(a.getSource() == item1) { // MenuItem 1 has an action event.
>        :
>      }
>
> 3. You can call a method into that IF, and you can make a new window with
> all JList content.
> 4. You can pass as parameter the MenuItem that was clicked.
>
>     showMyJList((JMenuItem)a.getSource());
>
> or
>
>     showMyJList(item1);
>
> 5. Into 'showMyJList', you can knows the MenuItem's text
>
>        void showMyJList(JMenuItem item) {
>          :
>          String label = item.getLabel(); // MenuItem's name that was   fired
> an event (clicked)
>          :
>                     showNewWindows();
>                     :
>        }
>
> You will need to do a review of Swing :P ...
>
> On Thu, Sep 24, 2009 at 5:33 AM, javaquestion
> <cplusplusquest...@gmail.com>wrote:
>
>
>
> > On Sep 24, 1:04 am, Victor Lutin <umg.victor.lu...@gmail.com> wrote:
> > > Mmmh ... You can add an ActionListener directly to MenuItem, You don't
> > need
> > > to use a JButton for that.
>
> > > Check Java Doc:
>
> > >http://java.sun.com/j2se/1.4.2/docs/api/java/awt/MenuItem.html
>
> > > This works in 1.4+ :) ...
>
> > > Your class most implements ActionListener Interface
>
> > > public class YourClasssWithMenuItems implements *ActionListener* {
> > >   private JMenuItem item1;
> > >   :
> > >   *public void actionPerformed(ActionEvent a)* {
> > >     if(a.getSource() == item1) { // MenuItem 1 has an action event.
> > >       :
> > >     }
> > >     :
> > >   }
> > >   :
> > >     item1 = new JMenuItem("Item 1");
> > >     item.addActionListener(this);
> > >   :
>
> > Thanks for this.  However, the program in some cases will work as
> > follows:
>
> > Click JMenuItem  -> Display a JList -> Select an item from JList ->
> > Display a new window, in this new window I need to check which
> > JMenuItem I was clicked.  I still don't know how to work.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Java EE (J2EE) Programming with Passion!" group.
To post to this group, send email to
java-ee-j2ee-programming-with-passion@googlegroups.com
To unsubscribe from this group, send email to
java-ee-j2ee-programming-with-passion+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to