• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic Concepts of AWT Event Generation  RSS feed

 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Swing and want to clarify some basic concepts. I know mouseEvent must somehow be generated by the JVM in response to Windows mouse click. But who is going to generate the ActionEvent? Is it the JComponents (if I am using swing) that have the code to generate the ActionEvent when they receive the MouseEvent. If I want to write my own graphic component, say a fancy clickable graphic button in a game, should my component class (which extends java.awt.Container) contain code to generate my own ActionEvent base on the MouseEvent. If, yes, where should these code be placed in?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I confess I am not sure I understand your question correctly. But I gather you are talking about how the event mechanism works and what all code you need to write in case of custom components/events.

In general your component should have the capacity to
1) Register listeners (e.g. addActionListener)
2) Identify when to generate an event (e.g. Mouse click)
3) Propogate the event to all registered listeners

You might find this a good read
http://www.exampledepot.com/egs/java.util/CustEvent.html

Does this answer your question or did I misunderstand what you meant to say?
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh,

Thanks for your reply. Yes, I am writing a custom component and the component need to recognise mouse click and response to the mouse in some special way. But one major concept I'm still not able to understand is that it is always said that the component generate the events (be it mouse, actionEvent or custom event). However, how it is possible for a component, written in Java, generate a Mouse Event ? For example, in Windows, the mouse click is controlled by Window device driver and when I click on an AWT component, it should be Windows that generate the mouse event and notify my component about that. How this map into the statement that the component generates a mouse event?

The second question is about the higher level event based on the mouse event, say ActionEvent of a button. It looks like it is my custom component, after RECEIVING the mouse click from the OS, to generate it. But where should I place the ActionEvent generation code in my customer component?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is possible for a component, written in Java, generate a Mouse Event ?

There is a bit of a confusion here. The component does not generate the event. All it does is trap the event and propagate it to all its registered listeners, either as a mouse event or a custom event. What is means is you will have to add a mouse listener to your custom component.

But where should I place the ActionEvent generation code in my customer component?

Check out the source code for AbstractButton especially addActionListener and fireActionPerformed
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!