Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Confused btwn actionPerformed and actionAdapter

1 view
Skip to first unread message

Daniel

unread,
Jan 2, 2008, 9:17:41 AM1/2/08
to

Dear fellow developers,

When I clicked a component for instance a button, the event handler code as in below appears under Source tab. Could anyone please explain to me how it works and why does the action_Performed method appear two times, once by itself as a method and then another time under a class unknown to me called actionAdapter? All I want to do is just code one event handler that handles all button clicks, like so:
public void button_actionPerformed(ActionEvent e, Object arg);
{ ...... etc }

Here's theh code generated by JBuilder when I double-clicked a button:
public void jButton10_actionPerformed(ActionEvent e) {
}
}

class Frame2_jButton10_actionAdapter implements ActionListener {
private Frame2 adaptee;
Frame2_jButton10_actionAdapter(Frame2 adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton10_actionPerformed(e);
}
}

Thanks in advance for explaining to me.

Daniel

unread,
Jan 2, 2008, 9:46:37 AM1/2/08
to

Hi all,

I also found out from Sun website that ActionListener does not have an adapter class. So why is the a declaration of this class in the event handler code?

class Frame2_jButton10_actionAdapter implements ActionListener { //etc }

Here's the link that says so: class
http://java.sun.com/docs/books/tutorial/uiswing/events/api.html

Kevin Dean [TeamB]

unread,
Jan 2, 2008, 3:00:18 PM1/2/08
to
Daniel wrote:

>Could anyone please explain to me how it works and why does the
>action_Performed method appear two times, once by itself as a method and
>then another time under a class unknown to me called actionAdapter?

In order for the button to have any effect, you have to register an
ActionListener object with the button. The JBuilder code is generating an
ActionListener, which it then registers with the button (look in the
jbInit() method for that code), and then provides a callout from the
ActionListener back to your Frame class to do the real work.

--
Kevin Dean [TeamB]
Dolphin Data Development Ltd.
http://www.datadevelopment.com/

Please see Borland's newsgroup guidelines at
http://info.borland.com/newsgroups/guide.html

Bruce Houghtron

unread,
May 17, 2008, 4:41:16 PM5/17/08
to
Keep it simple, go into the code under jbInit() and rewrite it like this;

JButton2 jButton2 = new JButton();
jButton2.setBackground(Color.pink);
jButton2.setBounds(new Rectangle(100, 188, 85, 32));
jButton2.setFont(new java.awt.Font("Dialog", 1, 14));
jButton2.setText("Cats");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
Cats();
}
});

JButton1 jButton2 = new JButton();
jButton1.setBackground(Color.green);
jButton1.setBounds(new Rectangle(100, 120, 85, 32));
jButton1.setFont(new java.awt.Font("Dialog", 1, 14));
jButton1.setText("Dogs");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
Dogs();
}
});


an
"Daniel " <dan...@gmail.com> wrote in message
news:477bab15$1...@newsgroups.borland.com...

Bruce Houghtron

unread,
May 17, 2008, 4:59:29 PM5/17/08
to

> Keep it simple, go into the code under jbInit() and rewrite it like this;
>
> jButton2 JButton = new JButton();

> jButton2.setBackground(Color.pink);
> jButton2.setBounds(new Rectangle(100, 188, 85, 32));
> jButton2.setFont(new java.awt.Font("Dialog", 1, 14));
> jButton2.setText("Cats");
> jButton2.addActionListener(new java.awt.event.ActionListener() {
> public void actionPerformed(ActionEvent e) {
> Cats();
> }
> });
>
> jButton1 JButton = new JButton();

Bruce Houghtron

unread,
May 17, 2008, 5:02:11 PM5/17/08
to

"Bruce Houghtron" <bho...@globalserve.net> wrote in message
news:482f4730$1...@newsgroups.borland.com...

>
>> Keep it simple, go into the code under jbInit() and rewrite it like
>> this;
>>
>> JButton jButton2 = new JButton();

>> jButton2.setBackground(Color.pink);
>> jButton2.setBounds(new Rectangle(100, 188, 85, 32));
>> jButton2.setFont(new java.awt.Font("Dialog", 1, 14));
>> jButton2.setText("Cats");
>> jButton2.addActionListener(new java.awt.event.ActionListener() {
>> public void actionPerformed(ActionEvent e) {
>> Cats();
>> }
>> });
>>
>> JButton jButton1 = new JButton();
0 new messages