• Post Reply Bookmark Topic Watch Topic
  • New Topic

Event methods in Interface Objects  RSS feed

 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am just learning about GUI in Java. I got stuck on an exercise called 'Sharpen your pencil'. The exercise wants you to connect the widgets with the event methods.

I have 1 question upfront which I could not find in the Java API. Is there a possibility to see which interface objects contain which event methods? E.g. JButton contains methods x(), y() and z(). On the Java API page of JButton are a lot of methods includes but none of these events listed in the 'Sharpen your pencil' excercise. I am pretty sure that mousePressed() is an event method in the Jbutton interface object, however it is not listed on this page attached. Therefore, I conclude that not all methods are included here. Is there a place where I can find this?

Widgets
Check box
text field
scrolling list
button
dialog box
radio button
menu item

Event methods
windowClosing()
actionPerformed()
itemStateChanged()
mousePressed()
keyTyped()
mouseExcited()
focusGained()

new.PNG
[Thumbnail for new.PNG]
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have some nomenclature mixed up.
In Java the components are called:

Widgets - JComponent
Check box - JCheckBox
text field - JTextField
scrolling list - JList
button - JButton
dialog box - DialogBox (Usually an instance will be obtained via one of the many convenient showXXXDialog method in the JOptionPane class)
radio button - JRadioButton
menu item - JMenuItem
*Notice the 'J' prefix. That indicates a Swing component which supersedes the original AWT component. you should be using Swing. AWT is obsolete. (Some say Swing too is obsolete but thats a different story for another day  )

Swing uses something called an Event Delegation Model
You have an event source
You have an event listener.
You, as a developer, typically introduce these two entities to each other usually by something like
eventSource.addXXXListener(eventListenerInstance);
Note: You can add multiple event listeners to a single event source.
Also, you can add the same listener to multiple sources.

So lets say you have a button called "Ignition". When the user clicks this, you want to, well fire off your rocket.
So you would do something like

Once you do this, when the user clicks the igniteButton, the JVM will run your code inside the actionPerformed(ActionEvent e)

This might sound like a convoluted way of doing things. But think about it. What is a "click"?
For a mouse, it is a mouse press AND and a mouse release within the bounds (borders) of the event source, in this case the igniteButton. ALSO the user can tab over to the button and hit space/return key. That also generates a "click". All this hard work of monitoring and checking things is done for you by the JVM.

Are there different event type?
Sure. You have an action event, mouse event, scroll event, focus event and many more. All descend from the AWTEvent. If you check the API docs you will see all classes which provide different type of events.

Different components generate different events. How do you find which ones? Again API docs to the rescue. Look for a method called addXXXListener.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!