Adding ActionListener to JMenu

Is it possible to add actionListener to Jmenu object
for example File, Format, Exit these are Menus added in the menuBar not menu items
on clicking the Exit the frame shuld get closed.
For detecting the action clicked on the Menu shall i use actionListener?
I have tried using MenuListener its getting invoked on selection
I dont want it on selection , I want the frame to be displayed only on clicking that menu.

Alas, whilst a JMenu is-a JMenuItem (the Composite Pattern at work), it doesn't function entriely like on,
and registering ActionListeners with a JMenu doesn't work. Try MenuListener:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ActionExample {
    public static void main(String[] args) {
        Action sample = new SampleAction();
        JMenu menu = new JMenu("Menu");
        menu.setMnemonic(KeyEvent.VK_M);
        menu.add(sample);
        menu.addMenuListener(new SampleMenuListener());
        JToolBar tb = new JToolBar();
        tb.add(sample);
        JTextField field = new JTextField(10);
        field.setAction(sample);
        JFrame f = new JFrame("ActionExample");
        JMenuBar mb = new JMenuBar();
        mb.add(menu);
        f.setJMenuBar(mb);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(tb, BorderLayout.NORTH);
        f.getContentPane().add(field, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
class SampleMenuListener implements MenuListener {
    public void menuSelected(MenuEvent e) {
        System.out.println("menuSelected");
    public void menuDeselected(MenuEvent e) {
        System.out.println("menuDeelected");
    public void menuCanceled(MenuEvent e) {
        System.out.println("menuCanceled");
class SampleAction extends AbstractAction {
    public SampleAction() {
        super("Sample");
        putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("alt S"));
        putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_S));
        putValue(SHORT_DESCRIPTION, "Just a sample action");
    public void actionPerformed(ActionEvent evt) {
        System.out.println("sample...");
}

Similar Messages

  • Added actionListener, then GUI broke

    Hi,
    1. first i ran my menu GUI, it worked fine
    2. then i added the actionListener code... now she no workie...
    what did i do wrong???
    ERROR MSG:
    C:\jLotto\LotFrame.java:34: cannot resolve symbol
    symbol : class ActionListener
    location: class LottoFrame
              new ActionListener(){
    ^
    1 error
    Tool completed with exit code 1
    import javax.swing.JFrame;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.KeyStroke;
    import java.awt.Event;
    import javax.swing.JOptionPane; //print menu trial run
    public class LotFrame extends JFrame
      // Constructor
      public LotFrame(String title)
        setTitle(title);                             // Set the window title
        setDefaultCloseOperation(EXIT_ON_CLOSE);     // handle exit operation
        setJMenuBar(menuBar);                        // Add the menu bar to the window
        //MAIN MENU
        JMenu fileMenu    = new JMenu("File");          // Create File menu
        JMenu findMenu    = new JMenu("Find");
        // Construct the file pull down menu
        newItem   = fileMenu.add("New");             // Add New item
        openItem  = fileMenu.add("Open");            // Add Open item
        closeItem = fileMenu.add("Close");           // Add Close item
        fileMenu.addSeparator();                      // Add separator
        saveItem  = fileMenu.add("Save");            // Add Save item
        saveAsItem= fileMenu.add("Save As...");      // Add Save As item
        fileMenu.addSeparator();                      // Add separator
        printItem.addActionListener(
               new ActionListener(){
                    public void actionPerformed( ActionEvent e )
                         JOptionPane.showMessageDialog( LotFrame.this,
                         "adding actionListener at end of GUI setup",
                         "action at end", JOptionPane.PLAIN_MESSAGE);
                    }//end of actionPerformed
              }//end of actionLinstener
         );//endof .addActionListener
       printItem = fileMenu.add("Print");           // Add Print item
        menuBar.add(fileMenu);                       // Add the file menu
        menuBar.add(findMenu);
      }//end of constructor
      private JMenuBar menuBar = new JMenuBar();     // Window menu bar
      // File menu items
      private JMenuItem newItem, openItem, closeItem, saveItem, saveAsItem, printItem;
    }//end of class LotFrame

    java.awt.event.*;
    import java.awt.Event;

  • Adding actionListener to JComboBox

    Hi there
    I have added actionListener to a JComboBox. I only want the actionPerformed() get called when the user choose the item in the JComboBox, however, it also get called whenever I change the items in the JComboBox.
    I have tried to call the ActionEvent.getID() method to identify the action, however, changing the items and choosing the item give me the same ID - 1001.
    Has anyone got this problem before? How did you solve it?
    Should I use other Listener for listening only the choosing event?
    In advance thanks!
    From
    Edmond

    I had many problems using actionListeners with JComboBox because it gets called all the time, I have had much better luck using mouseListener and the events getClickCount() method for single and double click selections, this avoided many of the problems for me.

  • Adding actionlistener to JTextArea

    I'm trying to add an actionListener to my JTextArea, so that when the user hits 'enter', the action occurs.
    sort of like this:
    JTextArea.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ??????) {
    doSomethingToTextArea();
    but I'm having problems (e.g. how to make enter the action event, and some problem that says JTextArea can't use actionlisteners???)
    any ideas?
    thanks,
    n00bProgrammer

    Like serveral of the more complex swing gadgets the JTextarea uses model/view architecture. In this case the model behind the textarea is a Document, and the text change listener needs to be added to the Document, not the gadget itself. i.e. area.getModel().addDocumentListener();
    However JTextArea still inherits the methods of java.awt.Component including addKeyListener();

  • Adding actionlistener to menuItem

    hi...i'm trying to add an actionListener to a menuItem. i looked at the tutorial on "how to use menus" but i cant seem to execute the menuitem. here's my code...
    public class Controller implements ActionListener
        GUI gui;
        public Controller(GUI g)
            gui = g;
            gui.menuItem.addActionListener(this);
        public void actionPerformed(ActionEvent e)
            gui.menuItem = (JMenuItem)(e.getSource());
            if (gui.menuItem.getText().equals("New Database"))
                System.out.println("Hello");
    }where the class GUI is where the menItems have been added to the menus. i'm just trying out for one menuItem first but i wont get the "hello" printed on the screen...can someone give me some assistance on this given that i havent used JMenuItems before...thx in advance...

    As carnickr said we really need the source from GUI to be able to help you more. Even without looking at GUI it's apparent that you're methodology is lacking. Depending on design and requirements I can think of many different ways you might need to handle menu item actions. I can't think of a single one that would involve comparing the text, which is what you're doing.
    First of all, adding a listener to a public field of a different class is inherently the product of a flawed design or bad design itself. Your code should look more like this:
    JMenuItem newDatabase = new JMenuItem("New Database");
    JMenuItem openDatabase = new JMenuItem("Open Database");
    JMenuItem closeDatabase = new JMenuItem("Close Database");
    ActionListener listener = new ActionListener() {
         public void actionPerformed(ActionEvent e) {
              if (e.getSource() == newDatabase) {
                   System.out.println("New database clicked");
              } else if (e.getSource() == openDatabase) {
                   System.out.println("Open database clicked");
              } else fi (e.getSource() == closeDatabase) {
                   System.out.println("Close database clicked");
    newDatabase.addActionListener(listener);
    openDatabase.addActionListener(listener);
    closeDatabase.addActionListener(listener);Obviously an anonymous class is not always appropriate. The important thing here is that you're comparing the reference to see if it's the same Object, not trying to compare the text. If you post GUI and a little more about what you're trying to do we can try and give you some pointers on how to design it better.

  • Adding actionListener to a button

    pls i want to create an applet with buttons rectangle,circle,and polygon and having a textArea.when someone clicks on the rectangle buttons it draws a rectangle,on the circle buttons it draws a circle and on the polygon button it draws a polygon.pls i have already created the user interface how do i add the actionListeners to the respective buttons and reggister them.

    Hi,
    try something like this:
    JButton jButton1 = new JButton("yourbutton");
    jButton1.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                    someMethodThatDoesWhatYouNeed();
    });this is just a way to do it, there are other ways, just look around in the forum to discover them.
    JoY?TiCk

  • Adding ActionListener to JPanel or equivalent

    Hi all.
    I am trying to add an actionListener for mouse clicks on a JPanel, ideally using something like this:
    panel.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
              //do something
       });However, JPanel does not implement addActionListener, and as far as I can see the only components which do are buttons, radio buttons and the like.
    I tried extending JPanel to implement ActionListener, but had no success.
    Is there an easy way to monitor for mouse clicks on a JPanel or container? Bearing in mind there will be several of these containers at different locations, so using a global mouse listener would not be useful.
    Thanks

    Hi all.
    I am trying to add an actionListener for mouse clicks
    on a JPanel, ideally using something like this:
    panel.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    //do something
    });owever, JPanel does not implement addActionListener,
    and as far as I can see the only components which do
    are buttons, radio buttons and the like.
    I tried extending JPanel to implement ActionListener,
    but had no success.
    Is there an easy way to monitor for mouse clicks on a
    JPanel or container? Use a MouseListener.
    Bearing in mind there will be
    several of these containers at different locations,
    so using a global mouse listener would not be
    useful.So add individual MouseListeners to each container.
    >
    Thanks

  • Adding actionListener to JOptionPane

    how do you add an actionListener to a JOptionPane?
    im using JOptionPane.showMessageDialog, and i want an event to happen when the user clicks on the "Okay" button

    Can you do something like this instead? Use a showConfirmDialog(...) istead of the showMessageDialog(...), then use the return value to determine which button the user pushed?
    int retValue = JOptionPane.showConfirmDialog(parent, message, Title, JOptionPane.OK_CANCEL_OPTION );
    if (retValue == JOptionPane.OK_OPTION)
      doOkayEvent();
    else
      doNotOkayEvent();

  • Trouble adding actionlistener to a panel in a tabbed pane

    Hi, please help me! this is the code i wrote, with the error given below:
    CODE:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class tabslisten extends JApplet
         JPanel RecPanel;
         GridBagLayout gbl;
         GridBagConstraints gbc;
         public void init ()
              FlowLayout flow;
              flow = new FlowLayout();     
              Container content = getContentPane();
              content.setLayout(new GridLayout());
              JTabbedPane tabpane = new JTabbedPane();
              content.add(tabpane, flow);
              RecPanel = new JPanel();
              recipientDetails();
              tabpane.addTab("Recipient Details", null, RecPanel, "Recipient Details");     
         public void recipientDetails()
              JLabel lblRecFName;
              JLabel lblRecLName;
              JTextField txtRecFName;
              JTextField txtRecLName;
              JButton btnValidate;
              gbl = new GridBagLayout();
              gbc = new GridBagConstraints();
              RecPanel.setLayout(gbl);
              lblRecFName = new JLabel("First Name");
              lblRecLName = new JLabel("Last Name");
              txtRecFName = new JTextField(8);
              txtRecLName = new JTextField(5);
              btnValidate= new JButton("Validate");
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 1;
              gbc.gridy = 8;
              gbl.setConstraints(lblRecFName, gbc);
              RecPanel.add(lblRecFName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 4;
              gbc.gridy = 8;
              gbl.setConstraints(txtRecFName, gbc);
              RecPanel.add(txtRecFName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 1;
              gbc.gridy = 11;
              gbl.setConstraints(lblRecLName, gbc);
              RecPanel.add(lblRecLName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 4;
              gbc.gridy = 11;
              gbl.setConstraints(txtRecLName, gbc);
              RecPanel.add(txtRecLName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 4;
              gbc.gridy = 32;
              gbl.setConstraints(btnValidate, gbc);
              RecPanel.add(btnValidate);
              validateAction validateButton = new validateAction();
              btnValidate.addActionListener(validateButton);
         class validateAction implements ActionListener
              public void actionPerformed(ActionEvent evt)
                   Object obj = evt.getSource();
                   if (obj == btnValidate)
                        String fName = txtRecFName.getText();
                        String lName = txtRecLName.getText();
                        if (fName.length()==0)
                             getAppletContext().showStatus("First Name not entered.");
                             return;
                        else if (lName.length()==0)
                             getAppletContext().showStatus("Last Name not entered.");
                             return;
                        else
                             getAppletContext().showStatus("Successful.");
    ERROR:
    tabslisten.java:90: Undefined variable: btnValidate
    if (obj == btnValidate)
    ^
    tabslisten.java:92: Undefined variable or class name: txtRecFName
    String fName = txtRecFName.getText();
    ^
    tabslisten.java:93: Undefined variable or class name: txtRecLName
    String lName = txtRecLName.getText();
    ^
    3 errors
    Press any key to continue...
    I'm sure I'm doing something really stupid, but I don't know what! ANY and ALL help will be appreciated! Thanks!

    I copied your program and ran it. It works fine. Make sure the three variables you made class variables are only defined in one place. (ie you moved them not copied them). Other than that I have no ideas.
    With regards to naming conventions. My only comment is, take a look at the Java API.
    1) classes have uppercased words. Examples from your program - JTextField, Container, FlowLayout, GridLayout...
    2) methods and variable names don't upper case the first word - getContentPane(), setLayout()...
    Here is the program that works for me:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class FirstApplet extends JApplet
         JPanel RecPanel;
         GridBagLayout gbl;
         GridBagConstraints gbc;
         JTextField txtRecFName;
         JTextField txtRecLName;
         JButton btnValidate;
         public void init ()
              System.out.println("hello there");
              FlowLayout flow;
              flow = new FlowLayout();
              Container content = getContentPane();
              content.setLayout(new GridLayout());
              JTabbedPane tabpane = new JTabbedPane();
              content.add(tabpane, flow);
              RecPanel = new JPanel();
              recipientDetails();
              tabpane.addTab("Recipient Details", null, RecPanel, "Recipient Details");
         public void recipientDetails()
              JLabel lblRecFName;
              JLabel lblRecLName;
              gbl = new GridBagLayout();
              gbc = new GridBagConstraints();
              RecPanel.setLayout(gbl);
              lblRecFName = new JLabel("First Name");
              lblRecLName = new JLabel("Last Name");
              txtRecFName = new JTextField(8);
              txtRecLName = new JTextField(5);
              btnValidate= new JButton("Validate");
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 1;
              gbc.gridy = 8;
              gbl.setConstraints(lblRecFName, gbc);
              RecPanel.add(lblRecFName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 4;
              gbc.gridy = 8;
              gbl.setConstraints(txtRecFName, gbc);
              RecPanel.add(txtRecFName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 1;
              gbc.gridy = 11;
              gbl.setConstraints(lblRecLName, gbc);
              RecPanel.add(lblRecLName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 4;
              gbc.gridy = 11;
              gbl.setConstraints(txtRecLName, gbc);
              RecPanel.add(txtRecLName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 4;
              gbc.gridy = 32;
              gbl.setConstraints(btnValidate, gbc);
              RecPanel.add(btnValidate);
              validateAction validateButton = new validateAction();
              btnValidate.addActionListener(validateButton);
         class validateAction implements ActionListener
              public void actionPerformed(ActionEvent evt)
                   Object obj = evt.getSource();
                   if (obj == btnValidate)
                        String fName = txtRecFName.getText();
                        String lName = txtRecLName.getText();
                        if (fName.length()==0)
                             getAppletContext().showStatus("First Name not entered.");
                             return;
                        else if (lName.length()==0)
                             getAppletContext().showStatus("Last Name not entered.");
                             return;
                        else
                             getAppletContext().showStatus("Successful.");

  • Adding Actionlistener to a cell in a JTable?

    is this possible? how would i go among doing this?

    use the ListSelectionListener.
    In "valueChanged(ListSelectionEvent e)" you get the row with "e.getFirstIndex()".

  • Strange error while using ActionListener with RichCommandLink

    Hi,
    I am using Technology preview 3.
    I have RichTable bound to af:table in my JSF page.
    I am showing database contents inside richTable.
    Inside richTable i have one extra column in which i am showing remove link. So every row of table will have remove link. I have added ActionListener as inner class for the backing bean. and this actionlistener i am adding into RichCommandLink(remove link)
    But when i click on remove link I am getting weired exception. And i am not able to get why this error is coming.
    This problem occures whenever I use contentDelivery parameter with <af:table>
    Here is the stack trace of the error.
    java.lang.InstantiationException: com.backingBean.UpdateSampleTypeB
    ackingBean$MyLinkActionListener
    at java.lang.Class.newInstance0(Class.java:335)
    at java.lang.Class.newInstance(Class.java:303)
    at org.apache.myfaces.trinidad.bean.util.StateUtils$Saver.restoreState(S
    tateUtils.java:286)
    at org.apache.myfaces.trinidad.bean.util.StateUtils.restoreStateHolder(S
    tateUtils.java:202)
    at org.apache.myfaces.trinidad.bean.util.StateUtils.restoreList(StateUti
    ls.java:257)
    at org.apache.myfaces.trinidad.bean.PropertyKey.restoreValue(PropertyKey
    .java:231)
    at org.apache.myfaces.trinidad.bean.util.StateUtils.restoreState(StateUt
    ils.java:148)
    at org.apache.myfaces.trinidad.bean.util.FlaggedPropertyMap.restoreState
    (FlaggedPropertyMap.java:194)
    at org.apache.myfaces.trinidad.bean.FacesBeanImpl.restoreState(FacesBean
    Impl.java:358)
    at org.apache.myfaces.trinidad.component.UIXComponentBase.restoreState(U
    IXComponentBase.java:881)
    at org.apache.myfaces.trinidad.component.UIXComponentBase.processRestore
    State(UIXComponentBase.java:861)
    at org.apache.myfaces.trinidad.component.TreeState.restoreState(TreeStat
    e.java:96)
    at org.apache.myfaces.trinidad.component.UIXComponentBase.processRestore
    State(UIXComponentBase.java:855)
    at org.apache.myfaces.trinidad.component.TreeState.restoreState(TreeStat
    e.java:96)
    at org.apache.myfaces.trinidad.component.UIXComponentBase.processRestore
    State(UIXComponentBase.java:855)
    at javax.faces.component.UIComponentBase.processRestoreState(UIComponent
    Base.java:1154)
    at org.apache.myfaces.trinidad.component.TreeState.restoreState(TreeStat
    e.java:96)
    at org.apache.myfaces.trinidad.component.UIXComponentBase.processRestore
    State(UIXComponentBase.java:855)
    at org.apache.myfaces.trinidad.component.TreeState.restoreState(TreeStat
    e.java:96)
    at org.apache.myfaces.trinidad.component.UIXComponentBase.processRestore
    State(UIXComponentBase.java:855)
    at org.apache.myfaces.trinidad.component.TreeState.restoreState(TreeStat
    e.java:96)
    at org.apache.myfaces.trinidad.component.UIXComponentBase.processRestore
    State(UIXComponentBase.java:855)
    at org.apache.myfaces.trinidad.component.TreeState.restoreState(TreeStat
    e.java:96)
    at org.apache.myfaces.trinidad.component.UIXComponentBase.processRestore
    State(UIXComponentBase.java:855)
    at javax.faces.component.UIComponentBase.processRestoreState(UIComponent
    Base.java:1154)
    at org.apache.myfaces.trinidadinternal.application.StateManagerImpl.rest
    oreView(StateManagerImpl.java:487)
    at com.sun.faces.application.ViewHandlerImpl.restoreView(ViewHandlerImpl
    .java:287)
    at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWra
    pper.java:193)
    at org.apache.myfaces.trinidadinternal.application.ViewHandlerImpl.resto
    reView(ViewHandlerImpl.java:258)
    at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._restoreView(Li
    fecycleImpl.java:438)
    at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._executePhase(L
    ifecycleImpl.java:229)
    at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl.execute(Lifecyc
    leImpl.java:155)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
    at com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterC
    hain.java:65)
    at oracle.adfinternal.view.faces.webapp.rich.SharedLibraryFilter.doFilte
    r(SharedLibraryFilter.java:135)
    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterL
    istChain.doFilter(TrinidadFilterImpl.java:284)
    at oracle.adfinternal.view.faces.webapp.rich.RegistrationFilter.doFilter
    (RegistrationFilter.java:69)
    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterL
    istChain.doFilter(TrinidadFilterImpl.java:284)
    at oracle.adfinternal.view.faces.activedata.ADSFilter.doFilter(ADSFilter
    .java:74)
    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterL
    istChain.doFilter(TrinidadFilterImpl.java:284)
    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl._invoke
    DoFilter(TrinidadFilterImpl.java:208)
    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl._doFilt
    erImpl(TrinidadFilterImpl.java:165)
    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl.doFilte
    r(TrinidadFilterImpl.java:138)
    at org.apache.myfaces.trinidad.webapp.TrinidadFilter.doFilter(TrinidadFi
    lter.java:92)
    at com.evermind.server.http.EvermindFilterChain.doFilter(EvermindFilterC
    hain.java:15)
    at oracle.adf.library.webapp.LibraryFilter.doFilter(LibraryFilter.java:1
    18)
    at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletReque
    stDispatcher.java:611)
    at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(Ser
    vletRequestDispatcher.java:362)
    at com.evermind.server.http.HttpRequestHandler.doDispatchRequest(HttpReq
    uestHandler.java:915)
    at com.evermind.server.http.HttpRequestHandler.doProcessRequest(HttpRequ
    estHandler.java:821)
    at com.evermind.server.http.HttpRequestHandler.processRequest(HttpReques
    tHandler.java:626)
    at com.evermind.server.http.HttpRequestHandler.processRequest(HttpReques
    tHandler.java:599)
    at com.evermind.server.http.HttpRequestHandler.serveOneRequest(HttpReque
    stHandler.java:383)
    at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.ja
    va:161)
    at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.ja
    va:142)
    at oracle.oc4j.network.ServerSocketReadHandler$ClientRunnable.run(Server
    SocketReadHandler.java:275)
    at oracle.oc4j.network.ServerSocketAcceptHandler.procClientSocket(Server
    SocketAcceptHandler.java:237)
    at oracle.oc4j.network.ServerSocketAcceptHandler.access$800(ServerSocket
    AcceptHandler.java:29)
    at oracle.oc4j.network.ServerSocketAcceptHandler$AcceptHandlerHorse.run(
    ServerSocketAcceptHandler.java:878)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExec
    utor.java:650)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
    .java:675)
    Can anybody please provide any help on this?
    Regards,
    Hiren

    Hi Simon,
    I am using addActionListener method of RichCommandLink
    Here is how i am trying to use it.
    public class backingBean {
         private RichTable table;
         public backingBean() {
         RichColumn rc = new RichColumn();
         RichCommandLink cmd = new RichCommandLink();
         MyActionListener listener = new MyActionListener();
         cmd.addActionListener(listener);
         public RichTable getTable() {
         return table;
         class MyActionListener implements ActionListener {
              public void processAction (ActionEvent actionEvent) throws AbortProcessingException {
              // Processing related to edit components of backing bean
    Hiren

  • Event handling in JMenu in JPanel

    Hi,
    I'm writing a small register program GUI. I have a problem with ActionListener in JMenu. I wrote a JPanel which creates a JMenu and that works just fine. The problem is that id doesn't give any event signals. I have another class which impelemets ActionListener and it works fine with JButton (another panel for buttons), but not with JMenu.
    Below is the JPanel for JMenu and Application which implements the ActionListener. Do you see anything wrong? I really can't imagine why I cannot get the action signals. JPanel is defined in JFrame as it should be. Have you got any ideas?
    Regards,
    Marko
    class MenuPanel extends JPanel {
         protected JMenuBar menuBar;
         protected JMenu menu;
         protected JMenuItem loadRegister;
         protected JMenuItem saveRegister;
         protected JMenuItem information;
         protected JMenuItem quit;
         public MenuPanel(Application application) {
              menuBar = new JMenuBar();          
              menu = new JMenu("Tiedosto");     
              loadRegister = new JMenuItem("Lataa");
              loadRegister.addActionListener(application);
              saveRegister = new JMenuItem("Talleta");
              saveRegister.addActionListener(application);
              information = new JMenuItem("Tietoja");
              information.addActionListener(application);
              quit = new JMenuItem("Lopeta");                    quit.addActionListener(application);
              menu.add(loadRegister);
              menu.add(saveRegister);
              menu.addSeparator();
              menu.add(information);
              menu.add(quit);          
              menuBar.add(menu);
    class Application implements ActionListener {     
         // Define CardPanel
         private CardPanel cardPanel;
         // Define linked list for cards
         private CardList cardList = null;
         // Constructor. Initializes cardPanel and cardList
         public Application(CardPanel cardPanelRefrence) {
              cardPanel = cardPanelRefrence;
              cardList = new CardList();
         public void actionPerformed(ActionEvent action) {
              String actionPerformed = action.getActionCommand();
              if (actionPerformed.equals("Lataa")) {
                   load();          
              if (actionPerformed.equals("Talleta")) {
                   save();          
              if (actionPerformed.equals("Tietoja")) {
                   info();          
              if (actionPerformed.equals("Lopeta")) {
                   System.exit(0);
    // Continues with the methods...

    Well, I found the answer. Heh, quite easy one but it took a while before I found it..
    Just if you ever have similar problem. The problem was that in my JFrame I didn't create an object of class (Application) which implement s the ActionListener before using it as a reference in creating JPanel object. Quite confusing explanation, but here the code. This code was in my JFrame which was not included in this question.
    private Application application;
    private MenuPanel menuPanel;
    application = new Application(cardPanel, rightInfoPanel);
    menuPanel = new MenuPanel(application);
    These two were introduced other way round, so that variable "application" was null. What a problem in fact.. :)
    - Marko

  • Bug? Unable to add ActionListener using Anonymous class.

    Hi,
    I come accross one strange behaviour while adding ActionListener to RCF component.
    I am trying to add the ActionListener in the managed bean using the Anonymous.
    We can add the actionListener to a button using following methods. I am talking about the the first case. Only this case is not working. Rest other 2 cases are working properly.
    Case 1:
    class MyClass {
         RichCommmandButton btnTest = new RichCommmandButton();
         public MyClass(){
              btnTest.addActionListener(new ActionListener(){
                   public void processAction(ActionEvent event){
    Case 2:
    class MyClass implements ActionListener {
         RichCommmandButton btnTest = new RichCommmandButton();
         public void processAction(ActionEvent event){
    <af:button binding="#{myClassBean.btnTest}" actionListener="#{myClassBean.processAction}"/>
    Case 3:
    class MyClass implements ActionListener {
         RichCommmandButton btnTest = new RichCommmandButton();
         public void addActionLister(){
              //Use EL to add processAction(). Create MethodBinding
              FacesContext facesContext = FacesContext.getCurrentInstance();
    ELContext elContext = facesContext.getELContext();
    ExpressionFactory exprfactory = facesContext.getApplication().getExpressionFactory();
              MethodExpression actionListener =
    exprfactory.createMethodExpression(elContext, "#{myClassBean.processAction}", null, new Class[] { ActionEvent.class });
              btnTest.setActionListener(actionListener);
         public void processAction(ActionEvent event){
    Java has provided good way to use the Anonymous classes while adding the listeners. It should work with the RCF also.
    Some how i found the case 1 usefull, as i can have as many buttons in my screen and i can add the actionListener in one method. Also it is easy to read. I dont have to see the JSPX page to find the associated actionListener method.
    Is it a bug or i am wrong at some point?
    Any sujjestions are welcome.
    - Sujay.

    Hello Sujay,
    As I said in my previous reply, you can try with request scope. In JSF you shouldn't use the binding attribute very often. I agree that anonymous class is nice, but don't forget that you might be dealing with client state saving here so it cannot be perfectly compared with Swing that maintains everything in RAM. What I think happens with you currently is the following:
    1. Bean is created and the button instance as well. The ActionListener is added to the button;
    2. The view is rendered and while it is, the binding attribute is evaluated, resulting in the get method of your bean being called;
    3. Since the method returns something different than null, the button instance created in 1. get used in the component tree;
    4. The tree's state is saved on the client, since your class isn't a StateHolder, nor Serializable, the StateManager doesn't know how to deal with it so it gets discarded from the saved state and maybe from the component itself (would have to debug the render view phase to be sure);
    5. The postback request arrives, the tree is restored. When the handler reaches the button, it call the bean that returns the same instance that was used in the previous tree (since not request scoped), which is BAD because the remaining of the tree is not made of the same object instances, but rather new deserialized ones. The component then gets updated from the client state saved in 4, this might also be where the listener get removed (again debugging would tell you this, but I would tend more with the previous possibility). Note that with a request scoped bean you would have to add the listener during the first get method call (by checking if the component is null) or in the constructor as you're doing right now. It would be a very clean way and you could give the request bean (and thus the listener) access to the conversation scoped bean through injection which is very nice as well.
    6. The invoke application phase occurs and the listener is no longer there.
    Btw, this isn't a rich client issue, more a specification one. I'm curious if it works in a simple JSF RI application, if it does then I guess it would be a bug in Trinidad and/or rich client state handling architecture (using FacesBean).
    Regards,
    ~ Simon

  • ActionListener not responding (JRadioButton)

    Hi all
    I have a JMenuBar with a couple of JMenu's. One of them has a couple of JRadioButtons which, when clicked, do not trigger an event.
    The situation ( simplified). I have 2 classes, MainClass.java and TheToolBar.java.
    MainClass.java:public class MainClass extends JApplet{
      protected TheMenuBar tmb; 
        private void jbInit() throws Exception {
              tmb = new TheMenuBar(new ToolBarAction());
             ....... // other things that make the frame visible
        public class ToolBarAction implements ActionListener{
             public ToolBarAction(){}
            public void actionPerformed(ActionEvent e)
               System.out.println("action....") ;
    }TheToolBar: import........
    public class TheMenuBar extends JMenuBar{
       public TheMenuBar(ActionListener a) {
          JMenu jm = new JMenu();
          RadioButton jmi = new JRadioButton();
          jmi.setText("click me");
          jmi.addActionListener(tba);
         jm.add(jmi) ;
         this.add(jm) ;
        // and init other JMs
    }This is basically what happens, and the result is that I cannot select the radio button. I wonder if anyone can see what I do wrong here (I have the feeling it is something very basic)
    Thnx a lot
    LuCa

    Update: I recompiled it with a 1.4.2 compiler, instead of a 1.5.x and it all worked!!
    I guess I use something which is deprecated!
    cheers
    LuCa

  • ActionListener in JMenuItem

    Hi!
    I'm having problems with adding ActionListener on JMenuItem. I do it like this:
        JMenuItem saveMenuItem = new JMenuItem();
        saveMenuItem.addActionListener(new ActionListener()
          public void actionPerformed(ActionEvent e)
            System.out.println("actionPerformed!");
            saveActionPerformed(e);       
        });No even when I select that menu item from my GUI, the action is not launched! Compiler does not give any errors, so it should be ok. Can anyone tell me what's the problem?? I'm using JDK 1.3.1-b24, btw.
    /jasurakk

    Hi jasurakka,
    just reading the source code, nothing seems to be
    wrong. May be there's a problem using an adapter class
    as listener. I suggest you try to write an own class
    implementing the ActionListener interface and then add
    this class as ActionListener to your menu items.
    Appears to me, that this is anyway the better
    solution, cause you create only one ActionListener
    listening for all the menu items you're adding instead
    of creating a new listener instance for each menu
    item. In the latter case, you have to add and check a
    unique action command for each item.Yeah, that's pretty much what I had before... But when I couldn't make it work, I tried to add own actionlistener for all menu items. Unfortunately it didn't help at all:=( Might you have any other ideas of what to do?

Maybe you are looking for