problem in Event handling

From the following code, i am unable to trap button & combo events. Surely, i am doing something wrong here. Please suggest me where exactly i am wrong.

publicvoid actionPerformed(ActionEvent evt){

if(evt.getSource() == JButton.class){

JButton button = (JButton)evt.getSource();

...............

...............

}

if(evt.getSource() == JComboBox.class){

JComboBox cb = (JComboBox)evt.getSource();

System.out.println("Inside combo event: " + selItem);

}

}

[849 byte] By [ysrpa] at [2007-11-26 18:23:30]
# 1

if(evt.getSource() == JButton.class)

This is wrong. The result of getSource() is going to be an instance of Component, or some subclass of that. But you are comparing it to JButton.class, which is an instance of Class. The two cannot possibly be equal.

Perhaps you should be doing this:if(evt.getSource() instanceof JButton)

But if it were up to me I would write two separate listeners, one for the JButton and the other for the JComboBox. Then you wouldn't have to do any such comparisons. And more to the point, you wouldn't be making one class have two different responsibilities. Bad design, that.

DrClapa at 2007-7-9 5:57:30 > top of Java-index,Java Essentials,Java Programming...
# 2

> From the following code, i am unable to trap button &

> combo events. Surely, i am doing something wrong

> here. Please suggest me where exactly i am wrong.

>

> public void actionPerformed(ActionEvent evt) {

> if(evt.getSource() == JButton.class) {

> JButton button = (JButton)evt.getSource();

>...............

>...............

> rce() == JComboBox.class) {

> JComboBox cb = (JComboBox)evt.getSource();

> System.out.println("Inside combo event: " +

> " + selItem);

>

> }

>

> }

>

Have you call 'addActionListener' method and implement your class with ActionListener ?

p_epia at 2007-7-9 5:57:30 > top of Java-index,Java Essentials,Java Programming...
# 3

> if(evt.getSource() == JButton.class)

This

> is wrong. The result of getSource() is going to be an

> instance of Component, or some subclass of that. But

> you are comparing it to JButton.class, which is an

> instance of Class. The two cannot possibly be equal.

>

> Perhaps you should be doing

> this:if(evt.getSource() instanceof

> JButton)

Thank you. This worked.

>But if it were up to me I would write

> two separate listeners, one for the JButton and the

> other for the JComboBox. Then you wouldn't have to do

> any such comparisons. And more to the point, you

> wouldn't be making one class have two different

> responsibilities. Bad design, that.

Sorry , i didnt understand what you said. Could you please help me making understand this?

ysrpa at 2007-7-9 5:57:30 > top of Java-index,Java Essentials,Java Programming...
# 4

> But if it were up to me I would write

> two separate listeners, one for the JButton and the

> other for the JComboBox. Then you wouldn't have to do

> any such comparisons. And more to the point, you

> wouldn't be making one class have two different

> responsibilities. Bad design, that.

I'll add to that. I usually see this when people have some similar functionality that they don't want to have to write in two different places. That's usually a sign that it shouldn't be in the event listener to begin with. Instead, extract that functionality to a method and then have two separate listeners that both invoke it. That way what each is causing to happen is separated from the listener responsible for detecting the events.

kablaira at 2007-7-9 5:57:30 > top of Java-index,Java Essentials,Java Programming...
# 5

> > But if it were up to me I would write

> > two separate listeners, one for the JButton and

> the

> > other for the JComboBox. Then you wouldn't have to

> do

> > any such comparisons. And more to the point, you

> > wouldn't be making one class have two different

> > responsibilities. Bad design, that.

>

> I'll add to that. I usually see this when people

> have some similar functionality that they don't want

> to have to write in two different places. That's

> usually a sign that it shouldn't be in the event

> listener to begin with. Instead, extract that

> functionality to a method and then have two separate

> listeners that both invoke it. That way what each is

> causing to happen is separated from the listener

> responsible for detecting the events.

I understood what you said. But i still dont understand where in my code i have similar functionality in two different places

ysrpa at 2007-7-9 5:57:30 > top of Java-index,Java Essentials,Java Programming...
# 6

> I understood what you said. But i still dont

> understand where in my code i have similar

> functionality in two different places

Maybe you don't have that. If that's the case then there's even more reason to separate it into two listeners. Why do you think you need -one- class to handle two -different- responsibilities? Instead of having one implementation to handle both JButton and JComboBox and do two different things for each why don't you just have two completely different implementations: one to handle JButton, one to handle JComboBox.

kablaira at 2007-7-9 5:57:30 > top of Java-index,Java Essentials,Java Programming...
# 7

> > I understood what you said. But i still dont

> > understand where in my code i have similar

> > functionality in two different places

>

> Maybe you don't have that. If that's the case then

> there's even more reason to separate it into two

> listeners. Why do you think you need -one- class to

> handle two -different- responsibilities? Instead of

> having one implementation to handle both JButton and

> JComboBox and do two different things for each why

> don't you just have two completely different

> implementations: one to handle JButton, one to handle

> JComboBox.

You mean to say that JButton events should be handled in one class and JCombobox events in another class?

ysrpa at 2007-7-9 5:57:30 > top of Java-index,Java Essentials,Java Programming...
# 8

> > > I understood what you said. But i still dont

> > > understand where in my code i have similar

> > > functionality in two different places

> >

> > Maybe you don't have that. If that's the case

> then

> > there's even more reason to separate it into two

> > listeners. Why do you think you need -one- class

> to

> > handle two -different- responsibilities? Instead

> of

> > having one implementation to handle both JButton

> and

> > JComboBox and do two different things for each why

> > don't you just have two completely different

> > implementations: one to handle JButton, one to

> handle

> > JComboBox.

>

> You mean to say that JButton events should be handled

> in one class and JCombobox events in another class?

Since they're handled differently, yes. Ideally a class has one responsibility, not two vaguely linked responsibilities that involve the same interface and are smashed together for convenience.

kablaira at 2007-7-9 5:57:30 > top of Java-index,Java Essentials,Java Programming...
# 9

> You mean to say that JButton events should be

> handled

> in one class and JCombobox events in another class?

>

> Since they're handled differently, yes. Ideally a

> class has one responsibility, not two vaguely linked

> responsibilities that involve the same interface and

> are smashed together for convenience.

I still dont understand what is the benefit out of this approach. One class have only one component say like JButton. Why a class should not have multiple components and have various events for various components?

I do understand the benefit of breaking the code in small parts. But how far it is correct to restrict it to only one listener or event?

Please correct me if i am wrong anywhere

ysrpa at 2007-7-9 5:57:30 > top of Java-index,Java Essentials,Java Programming...