swing event model

X

xdevel1999

If I have a button and a frame and I register an actionlistener with
that frame when I push the button the handler on
the frame via getSource know that button but if I register a
mouselistener with the frame it doesn't know the button
why???

Does java event model support event propagation (bubbling) or not? I'm
confused!

So if I have many buttons and wanted to register one listener only to
the frame and via the frame
handler know which buttons fire one event how can I do that?

the Java model isn't like javascript DOM event model?
 
E

Eric Sosman

If I have a button and a frame and I register an actionlistener with
that frame when I push the button the handler on
the frame via getSource know that button but if I register a
mouselistener with the frame it doesn't know the button
why???

What do you mean by "register an actionlistener with that frame?"
Frames and JFrames don't generate ActionEvents, so there's nothing to
listen for. You're probably talking about the ActionEvent generated
by a Button or JButton, but I don't understand what "register with
that frame" is supposed to mean. Please be more specific -- ideally,
please provide a short, complete code example.

Mouse actions (of various kinds) *do* make sense in the context
of a Frame or JFrame, but again: it's not clear what you're doing.
Does java event model support event propagation (bubbling) or not? I'm
confused!

Sorry; I don't know what you mean by "bubbling."
So if I have many buttons and wanted to register one listener only to
the frame and via the frame
handler know which buttons fire one event how can I do that?

Again, this "register to the frame" language is baffling. But
I think I understand part of your question: You want to know how to
use one ActionListener instance to handle ActionEvents from many
JButtons, and you wonder how the ActionListener can determine which
JButton has been pressed. That's easy: The ActionLister's
actionPerformed() method receives an ActionEvent object as its
parameter, and the ActionEvent has a getSource() method that returns
the object that generated the event -- in this case, the particular
JButton that was pressed.

ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
JButton button = (JButton)evt.getSource();
// now you know which JButton fired the event
}
};

button1.addActionListener(listener);
button2.addActionListener(listener);
// ...

The ActionEvent also has a getActionCommand() method that returns
the "action command" String associated with the JButton that was
pressed, so if all your JButtons have different action commands you can
easily tell them apart. (For that matter, if you've got two different
"Delete" buttons in different parts of your GUI, you can tag them with
the same action command and have the ActionListener do the same thing
with both of them.)
the Java model isn't like javascript DOM event model?

Sorry; I can't help you with Javascript.
 
J

John B. Matthews

xdevel1999 said:
If I have a button and a frame and I register an actionlistener with
that frame when I push the button the handler on the frame via
getSource know that button but if I register a mouselistener with the
frame it doesn't know the button why???

Does java event model support event propagation (bubbling) or not?
I'm confused!

So if I have many buttons and wanted to register one listener only to
the frame and via the frame handler know which buttons fire one event
how can I do that?

the Java model isn't like javascript DOM event model?

Cross-posted to <http://stackoverflow.com/questions/3735461>.
 
D

Daniel Pitts

If I have a button and a frame and I register an actionlistener with
that frame when I push the button the handler on
the frame via getSource know that button but if I register a
mouselistener with the frame it doesn't know the button
why???

Does java event model support event propagation (bubbling) or not? I'm
confused!

So if I have many buttons and wanted to register one listener only to
the frame and via the frame
handler know which buttons fire one event how can I do that?

the Java model isn't like javascript DOM event model?
No, Java Swing is nothing like JavaScript.

You are better off registering a different ActionListener for every
button, which kicks off the exact process that button is supposed to
kick off. That way you don't have a lot of "if (source == foo)" blocks.

Also note, that you can actually have an "Action" instance which defines
quite a bit. An "Action" can be added to a menu as an item, to a button
as a button, and to a toolbar. Each "Action" instance has an
actionPerformed method which gets called whenever the UI item is actuated.

<http://download.oracle.com/javase/6/docs/api/javax/swing/AbstractAction.html>

How-To:
<http://download.oracle.com/javase/tutorial/uiswing/misc/action.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top