🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #9520

Problem adding an ActionListener to a JButton

#9520
From: Jeff Marendo
Date: Tue, 07 Mar 2000 00:00
59 lines
2514 bytes
Hello,

I've got some code where I'm trying to initialize a button's state in a
class that extends from a JPanel.  I have private ActionListener members for
this class; and, within a 'setButton' type function, I'm trying to add the
ActionListener to the button, as well register a keyboard action.

The problem that I'm having is that the listener doesn't seem to be
registering (i.e. I can't execute the code in the actionPerformed method).
I thought that by having private members that this would give me global
scope in the class that would sidestep the local scope of the function I'm
using.  My code is below.  If anyone can see where I'm going wrong with
this, please let me know.  Thanks.

...
JButton insertBtn = new JButton();
...
// in jbInit()
setInsertBtn(insertBtn);
...
private void setInsertButton(JButton button)

    button.setPreferredSize(d); // d is a private member of type Dimension
    button.setBorder(null);

    // Create the ActionListener for this button

    insertBtnal = new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        insertBtn_actionPerformed(ae);
      }
    };

    button.addActionListener(insertBtnal);

    /* Register keyboard action for this button.  This is
     * necessary when using the Metal or Motif Look & Feel (as of Java2.0).
     */

    button.registerKeyboardAction(insertBtnal,
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
JComponent.WHEN_FOCUSED);
    button.registerKeyboardAction(insertBtnal,
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
JComponent.WHEN_FOCUSED);

    ImageIcon insertBtnImage = new
ImageIcon(getClass().getResource("images/insert.gif"));
    if(insertBtnImage != null) {
      button.setIcon(insertBtnImage);
    }
    else {
      System.out.println("The insert image wasn't found and insertBtnImage
is null");
    }
    this.add(button);
  }
...
private ActionListener insertBtnal;
Technical Details
Message-ID: <8a3nmf$54a7@bornews.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,e0b4324bb89268d4,start
X-Google-Attributes: gid111d45,public
From: "Jeff Marendo" <marendoj@commos.com>
Subject: Problem adding an ActionListener to a JButton
Date: 2000/03/07
Message-ID: <8a3nmf$54a7@bornews.borland.com>
X-Deja-AN: 594397887
X-Priority: 3
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200
Organization: CommTech Corporation
X-MSMail-Priority: Normal
Reply-To: "Jeff Marendo" <marendoj@commos.com>
Newsgroups: borland.public.jbuilder.java.swing