🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #15264

Re: Press Button Automatically

#15264
From: Henning Halfpap
Date: Tue, 29 Jun 1999 00:00
43 lines
2950 bytes
MJ wrote:
> 
> Does anyone know how I can "press" a button in my code...I have a button
> that does certain events when it's pressed, but I want these events to occur
> when my user presses enter as well...I know I can call actionPerformed on
> the ActionListener that's attached to my button, but I'm not sure how to
> construct the actionEvent...I figure it'd be easier to just press the button
> automatically...hope i made sense.

Usually in actionPerformed(java.awt.event.ActionEvent myEvent) you
determine the object the event came from by calling myEvent.getSource().
Depending on that you usually divert to a method which actually performs
the action for your object, e.g.

if (myEvent.getSource == myButton) actionMyButton(myEvent);

so the method actionMyButton(java.awt.event.ActionEvent myEvent) is the
one you want to call if the "Enter" Key is pressed. The way to do this
is really simple: in the method you process the Enter key with, you
simply create a new ActionEvent (which is no more than an object after
all) and hand it to the actionMyButton method. Even better, you can hand
it to the actionPerformed method and let that one pass it to
actionMyButton.

here's how to call actionPerformed:

java.awt.event.ActionEvent buttonEvent = new
java.awt.event.ActionEvent(myButton,
java.awt.event.ActionEvent.ACTION_PERFORMED, null);
actionPerformed(buttonEvent);

This is effectively nothing more than pressing the button. AFAIK it's
the only way to achieve what you want.

HTH, HAND,

Henning

-- 
Programmer, n.:
A human-like machine that converts caffeine into code.

E-Mail will be posted as I see fit.
Technical Details
Message-ID: <3778DB0B.2B59F8C9@logics.de>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <7laik7$rg0$1@mailint03.im.hou.compaq.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d6b,3deb09cf5370bdb2
X-Google-Attributes: gid111d6b,public
X-Google-Thread: 10bb9b,3deb09cf5370bdb2
X-Google-Attributes: gid10bb9b,public
X-Google-Thread: 113ae9,3deb09cf5370bdb2
X-Google-Attributes: gid113ae9,public
X-Google-Thread: fecd1,3deb09cf5370bdb2
X-Google-Attributes: gidfecd1,public
X-Google-Thread: f5d71,3deb09cf5370bdb2
X-Google-Attributes: gidf5d71,public
X-Google-Thread: 10a146,3deb09cf5370bdb2
X-Google-Attributes: gid10a146,public
X-Google-Thread: fa45f,3deb09cf5370bdb2
X-Google-Attributes: gidfa45f,public
X-Google-Thread: f74b3,3deb09cf5370bdb2
X-Google-Attributes: gidf74b3,public
From: Henning Halfpap <hh@logics.de>
Subject: Re: Press Button Automatically
Date: 1999/06/29
Message-ID: <3778DB0B.2B59F8C9@logics.de>
X-Deja-AN: 495204984
Content-Transfer-Encoding: 7bit
References: <7laik7$rg0$1@mailint03.im.hou.compaq.com>
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
Organization: Posted via the Nacamar Network
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language,comp.lang.java,comp.lang.java.api,comp.lang.java.help,comp.lang.java.machine,comp.lang.java.misc,comp.lang.java.programmer,microsoft.public.inetexplorer.ie4.java_applets