Layering GUI itemsTopGUI ComponentsEvent handling

Event handling

The program DoubleDrawing is interesting in that it has two pop-up menus that behave quite differently.

While the shapes menu is used to determine what kind of object to draw when the mouse is clicked, when the user selects a new color, the latest object drawn immediately has its color changed:

  // pop up menu to select colors
  def colorMenu: Choice = selectBox.options("red", "blue", "green", "yellow")
  append(colorMenu)
 
  // when user select new color, change color of newShape
  colorMenu.onChangeDo evt: Event ->
    match(colorMenu.selected)
      case "red" -> newShape.color := red
      case "blue" -> newShape.color := blue
      case "green" -> newShape.color := green
      case "yellow" -> newShape.color := yellow
  

The methodRequest of onChangeDo tells the system to perform the match statement whenever a new item is chosen on the color menu. When a new item is selected, the match statement uses the string selected to determine what color to make the object.

In general in order to get the program to respond to events associated with the object, we must associate a handler to the GUI component.

W do this by adding an action to be performed when the user interacts with the item. E.g.

    colorMenu.onChangeDo{ evt: Event -> ...}
    clearButton.onMousePressDo {mevt:MouseEvent ->
       canvas.clear
    }
    

Layering GUI itemsTopGUI ComponentsEvent handling