1

I am new to Java GUI. So say if I have a button and there is no event handler provided by me ,then who is going to handle that event? Does it related to event dispatcher thread event handler? please also explain what is the role of event listener although I have handler? Is event handling and exception handling work the same way? thanks regards

  • 4
    If you don't assign an event handler for your button, nobody is going to handle that event. Simple. – Raptor Jul 18 '13 at 08:02
  • 2
    If you click the button, an event is pushed onto the Event Queue, this is picked up by the Event Dispatching Thread and the registered listeners (to the queue) are polled as to weather they want to handle the event or not. If no one does, then no one cares (poor event) – MadProgrammer Jul 18 '13 at 08:03
  • so how to register for an event? – user2594479 Jul 18 '13 at 08:09

2 Answers2

2

Nobdy's gonna hear it. The event mechanism in Java Swing (and other GUI libs) is like the listener (publish / subscribe) pattern. If no listner is registered, no one will handle the event.

Take a look at this: http://www.programcreek.com/2009/01/the-steps-involved-in-building-a-swing-gui-application/. In general: the Publisher (e.g. a JButton) publishes many events: mouseclicks, hovers and so on. An arbitrary amount of subscribers (MouseAdapter,...) may register. Every time an event of the specific type is published (e.g. a MouseEvent) the right listner comes in to place and does it's action.

The publisher does not know how many and which subscribers are registered. In this way they are loosely coupled

Edit: this may also help http://www.freejavaguide.com/java_swing.html

Mirco
  • 2,940
  • 5
  • 34
  • 57
0

"I have a button and there is no event handler provided by me ,then who is going to handle that event?"

Answer: No One!!

For details on event listeners and handling, refer docs here.

Further this SO post will clafify all u want!

Community
  • 1
  • 1
rahulserver
  • 10,411
  • 24
  • 90
  • 164
  • This seems to be confusing because I have learnt that event is like an interrupt to the CPU. So in this case an interrupt is unhandled? – user2594479 Jul 18 '13 at 08:13
  • @user2594479 at low level, what events are like and how they are does not matters to java due to abstraction.The concept of event handlers and listeners remains the same – rahulserver Jul 18 '13 at 08:14