🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Problem adding an ActionListener to a JButton

#9439
From: Paul Furbacher [TeamB]
Date: Sat, 18 Mar 2000 00:00
112 lines
3708 bytes
Jeff Marendo wrote:

> It's likely that my thinking is off with how I'm interpreting the usage of
> global variables here.  Originally, I was creating my ActionListener within
> a method called setInsertButton like so [...].

I couldn't find anything which gave a clue as to why
you were having a problem.  So, I wrote the following
based on your code:


/**
 * Title:        <p>
 * Description:  <p>
 * Copyright:    Copyright (c) <p>
 * Company:      <p>
 * @author
 * @version 1.0
 */
package maredolistener;

import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;

public class Frame1 extends JFrame {

  boolean response = true;
  JButton jButton1 = new JButton();
  JTextField jTextField1 = new JTextField();
  GridBagLayout gridBagLayout1 = new GridBagLayout();

  public Frame1() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    jButton1.setText("Are you listening?");
    this.getContentPane().setLayout(gridBagLayout1);
    setInsertButton(jButton1);
    jTextField1.setMinimumSize(new Dimension(200, 21));
    jTextField1.setPreferredSize(new Dimension(200, 21));
    this.getContentPane().add(jButton1, 
            new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.NONE, 
            new Insets(0, 0, 9, 0), 0, 0));
    this.getContentPane().add(jTextField1, 
            new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.NONE, 
            new Insets(12, 12, 11, 11), 0, 0));
  }

  void setInsertButton(JButton button) {
    java.awt.event.ActionListener listener = 
      new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jButton1_actionPerformed(e);
        }
      };
    button.addActionListener(listener);

    button.registerKeyboardAction(
      listener,
      KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
      button.WHEN_FOCUSED
    );
    // You had this line next.  However, it causes an action when
    // the button is false, potentially overwriting the other action.
/*
    button.registerKeyboardAction(
      listener,
      KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
      button.WHEN_FOCUSED
    );
*/
  }

  void jButton1_actionPerformed(ActionEvent e) {
    jTextField1.setText( 
      ( response ) 
       ? "Yes, I always do." 
       : "Huh? Did you say something?"
    );
    response = !response;
  }

  public static void main(String [] args) {
    Frame frame = new Frame1();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
    frame.pack();
    frame.show();
  }
}


This works and should provide an example for how you should
proceed.  

Good luck.


Paul Furbacher (TeamB)
Technical Details
Message-ID: <38D40177.DA9BB8D2@home.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <8a3nmf$54a7@bornews.borland.com> <38C81C6A.DEA90251@home.com> <8ab2np$i8b5@bornews.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,e0b4324bb89268d4
X-Google-Attributes: gid111d45,public
From: "Paul Furbacher [TeamB]" <pnf@home.com>
Subject: Re: Problem adding an ActionListener to a JButton
Date: 2000/03/18
Message-ID: <38D40177.DA9BB8D2@home.com>
X-Deja-AN: 599322815
Content-Transfer-Encoding: 7bit
References: <8a3nmf$54a7@bornews.borland.com> <38C81C6A.DEA90251@home.com> <8ab2np$i8b5@bornews.borland.com>
X-Accept-Language: en,pdf
Content-Type: text/plain; charset=us-ascii
Organization: Another Netscape Collabra Server User
Mime-Version: 1.0
Reply-To: pnf@home.com
Newsgroups: borland.public.jbuilder.java.swing