Event handling |
So far we have seen that we can create and install a GUI component as follows:
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.
In order to get the program to respond to events associated with the object, we must also:
colorMenu.addActionListener(this);
public class DrawingProgram extends WindowController implements ActionListener { ... }
public void actionPerformed(ActionEvent event) { ... }
As an exercise, how would you modify this program so that when a new figure is drawn on the screen, its color is determined by the setting of the color menu at that time.
A solution is given by ColorfulDoubleComboBoxDrawing.
It is awkward having the combo boxes be on opposite ends of the window. We can put them both together on the bottom if we first insert them in a JPanel. A JPanel uses a FlowLayout manager - meaning items just fit in the panel from left to right - they don't get put in NORTH, SOUTH, etc., and there is no contentPane to worry about in a JPanel. See PanelComboBoxDrawing.
Event handling |