Graphics – Part 4

Part 4 – Unit 5: Graphics (OPTIONAL: Computer Science 30S)

If you don’t feel like reading this web page, here are two alternatives:

  • Read this presentation of the same content (slides 37 – 46)
  • Watch this playlist of videos of the same content on YouTube

Content Sections:

  1. Dealing with Action Events

Dealing with Action Events

  • GUI containers and controls are “event driven” which means they “listen” for a user to do an “event” and then “react” to the event
  • For example a button is “listening” for the user to “click” it (the action) and the “reacts” by (for example) exiting
  • These 3 parts are needed for actions:
  1. An object to “listen” for events (called a “event listener” or action listener)
  2. The object being “listened” to (called the event objectthat we have to “add” the listener to
  3. The action (a method) that will be invoked (called) when the event occurs


  • Lets create a second example…


  • But this time in the constructor we’ll use some methods to do the tasks of setting up the GUI container and controls, and a method we will use to add actions
  • These are the same commands we had done in the previous example but now separated into methods (and without the word “this” which is not needed)


  • Here the GUI controls are instantiated using their constructor methods
  • Here we add them to the content pane and then set the “bounds” which sets the x,y position and the width and height in one method
  • This was because we had set the layout to null and needed to tell the container exactly where and the size to place the controls


  • We start by ( 1 ) creating a ActionListener object (which is imported) and use the NetBeans IntelliSense to autocomplete the constructor as it will help write a lot of this code


  • This creates an anonymous inner class (class object without a name) with a method called actionPerformed where we write code to set the text of the label


  • Now we ( 2 ) add this action listener to the button object so it will start “listening” for the action of “clicking” the button


  • Now continuing the addActions method, we add a key listener to listen for keyboard events


  • But this time, instead of creating a KeyListener object first (which we could have done), we make it anonymously inside the parameter of adding it to the textbox object


  • We also make use of the parameter e which was a KeyEvent passed from the KeyListener which can give us information like what key was pressed
  • Events that aren’t used (keyTyped and keyReleased) are still left as empty (stub) methods (they cannot be removed)


  • Now continuing the addActions method, we add a mouse listener to listen for mouse events
  • This time, we will again make an anonymous listener object inside the parameter, while adding it to the label object
  • Also, we will call another method we will make private in the class and shift our logic to that method (with arguments sent along)


  • This method takes two parameters and displays information about the mouse position when it is over the label or when it moves out of the label area


  • These same events can be done in the NetBeans editor usually by double clicking (with buttons) an object, or right clicking the object you want “listened to” and going to “Events” the specific event you want “listened” for, then writing the corresponding action


  • So using the editor, double clicking some objects, or right clicking some objects (including the JFrame) will get you to the code and will write the action listeners for you


If you understood this content, you should move forward to the examples page, or go back to the content page and look it over again!