Description
Now that you have the application window, it is an excellent time to learn how to provide the functionality to those buttons. The buttons in our application cannot do anything when they are clicked.
To make the button come alive, add an action listener to it. An action listener can be added using the addActionListener() method for each button declared in our application. You can use action listeners to handle events, such as what must happen when the end user clicks a button in the application window.
ActionListener is an interface, and addActionListener() allows you to bind the interface to each button.
Once you handle the button click event, you must define what happens when a particular button is clicked. To explain that, you need to implement the logic for the actionPerformed() method, which comes in the ActionListener interface. In this stage, after clicking the button, a new screen must appear on your window with available functionality to set reminders.
If you have a reminder to have a cup of water every four minutes, then your new screen should have a text field to add text It's time to drink some water.
This screen should have two buttons: OK and Cancel. OK confirms and sets a new reminder. Cancel aborts setting the reminder. The buttons in this stage take users back to the original application screen. Do not worry about the reminder timer logic in this stage; just implement the logic of both buttons.
Add two JComboBoxes to the screen to select the timer delay and the period in the following stages.
It is now time to add listeners to your menu options and buttons to make your reminder app do some real work. We recommend writing your project using the MVC pattern starting from this stage.
Objectives
Add addActionListener() to the ADD button with the name Add Reminder . It creates a new screen with the name Set Reminder. The title of this window should be Set Reminder . ADD button should be disabled after pressed.
The window should contain the following components:
JTextFieldwith theFieldname;JLabelReminder Text with theReminder Text Labelname. This label should be located over theFieldJTextField;JButtonOKwith theOKname;JButtonCancelwith theCancelname;
JComboBoxwith theset Periodname;JLabelwith thePeriod labelname. This label should be located beneath theset PeriodJComboBox;JLabelSet Periodwith theSet Repeat Period Labelname. This label should be located over theset PeriodJComboBox;JComboBoxwith theset Delayname;JLabelSet Delaywith theSet Delay Labelname. This label should be located overset DelayJComboBox;JLabelwith theDelays Labelname. This label should be located beneath theset DelayJComboBox.
Figures in the Set Delay comboBox and period Delay comboBox should follow this order:
For set delay – 30, 25, 15, 5;
For set Period – 0, 5, 10, 20;
This order will be used for testing purposes of your application.
Examples
Clicking on the OK or Cancel buttons close the Set Reminder window; only the Reminder application window is visible: