More Panels and GUI ComponentsTopAnnouncementsEvent handling

Event handling

So far we have seen that we can create and install a GUI component as follows:

  1. Create the item and initialize it if necessary.
  2. Add the items to the content pane of the WindowController, and validate the pane.

We also saw that we could set the program up so that the WindowController object is notified whenever the user interacts with a GUI item. The program DoubleComboBoxDrawing illustrates that with the color menu.

In order to get the program to respond to events associated with the object, we must also:

  1. Add this as a listener to the GUI item. E.g.,
        colorMenu.addActionListener(this);
        
  2. Add a declaration that the class implements the appropriate listener interface. E.g.,
        public class DrawingProgram extends WindowController 
                                    implements ActionListener { ... }
        
  3. Add the method promised by the listener interface:
        public void actionPerformed(ActionEvent event) {
           ...
        }
        

As we saw last time with PanelComboBoxDrawing, we can put multiple items in different parts of the window by inserting them into JPanels and then inserting the JPanels into the slots in the window.


More Panels and GUI ComponentsTopAnnouncementsEvent handling