tags:

views:

25

answers:

1

I read that a JButton implements ItemSelectable and into documentation it has the method addItemListener so I can argue that it can generate an ItemEvent... but when I register with a JButton (but also for a JMenuItem) that interface the event is not rised?

Why?

I understand that if into docs is reported that a component has an add....Listener it means that it support that event... but for experience isn't often so...

What's the truth?

+1  A: 

There is a difference between a button being "pressed" (which fires an ActionEvent) and a button being "selected" (which fires the ItemEvent). By default a JButton is backed by a javax.swing.DefaultButtonModel. If you look at the setPressed and setSelected methods in default button model you'll see the code that fires the different events.

So if you programmically call JButton.setSelected, your item listener will be fired. If you click the button, you'll only get the action event.

Note also that with a JButton (unlike, say, a JToggleButton) you probably won't see much visually when it is selected.

Ash