You are on page 1of 6

GUI Tutorial Part Two

onny Nadolny

Introduction
This tutorial will teach you about Graphical User Interfaces, or GUIs. GUIs are important because its an effective way for the user to interact with the program. The way Java allows you to create GUIs is the Abstract Windowing Toolkit, or AWT. Up until now youve only learned how to make a nice looking GUI, what youll learn now will teach you how to make them actually *DO* stuff! You do this using the java.awt.event.* package. Well, time to start learning events!

Table of Contents Section Setting Up Your Program Let Your Buttons Be Heard! How To Handle Events Check Box/Radio Button Events Text Field Events Miscellaneous Conclusion Page # 3 3 4 5 6 6 6

Setting Up Your Program To receive events, first you have to include some packages and an interface or two. An interface is something which adds to your programs functionality. First, you must import the java.awt.event.* package:
import java.awt.event.*;

Next, you need to include one of the EventListener interfaces. This applet includes the ActionListener interface:
public class MyApplet extends java.applet.Applet implements ActionListener {

The EventListener interfaces enable a component of a graphical user interface to generate user events. Without one of the listeners in place, a component cannot do anything that can be heard by other parts of a program. A program must include a listener interface for each type of component it wants to listen to. If you need to include multiple interfaces, separate them with a coma:
public class MyApplet extends java.applet.Applet implements ActionListener, MouseListener {

Well, thats all you need to do to set up your program to listen for events. Next you need to make your components generate the events. Let Your Buttons Be Heard! After you let your program listen for the events, your components must generate them. To let a component generate an event, you use the addActionListener() method. For example, to make a button generate events, you would type:
Button clearForm = new Button(Clear); clearForm.addActionListener(this);

That code produces a regular looking button which generates an event. The this statement means the current class. The code creates a button and tells it to direct all events to this class to be dealt with.

Table Of Contents
3

How To Handle Events When an event is generated by a component, it is sent to the specified class to be handled. Each EventListener has its own method to be called. For example, with the clear form button the method that would be called is the actionPerformed() method. This is what the method looks like:
public void actionPerformed(ActionEvent evt) { // method goes here }

If there is only one component that will generate an event, you can put the code to handle that objects event right in the method. However, if there is more than one object that can generate an event, youll need to find out which one did it. To do that you can use the getActionCommand() function:
String cmd = evt.getActionCommand();

The getActionCommand() sends back a string. If the object is a button, itll be the caption of that button. If its a text field, itll send the text. To get the actual object that created the event, you can use the getSource() function. This will decide which object generated the event, between a Button named fred, a text field named Texty, and another text field named James (hey, I was feeling creative):
public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); if (source == fred) { //Fred (the button) caused the event } else if (source == Texty) { //Texty (the text field) caused the event } else if (source == James) { //James (the other text field) cause the event } }

The getSource() function works with all objects to see which one caused the event.

Table Of Contents
4

Check Box/Radio Button Events Check boxes and radio buttons (aka choice boxes) require a different ActionEvent interface namely the ItemListener interface. The following code produces a check box and adds the item listener stuff:
Checkbox goodTutorial = new Checkbox("Good tutorial?", true); goodTutorial.addItemListener(this);

Also, checkboxes receive events through the itemStateChanged() method. To see which checkbox caused the event, you can use the getItem() method. To check what value the checkbox or radio button has, you can use the getState() method. This will check which of two checkboxes caused the event and what value they are in:
public void itemStateChanged(ItemEvent event) { String command = (String) event.getItem(); if (command == "Item One's Caption") { boolean value = item1.getState(); if (value == true) { //It's selected showStatus("Item 1 true"); } else { //It's not selected) showStatus("Item 1 false"); } } else if (command == "Item Two's Caption") { boolean value = item2.getState(); if (value == true) { //It's selected showStatus("Item 2 true"); } else { //It's not selected) showStatus("Item 2 false"); } } }

Yah... that code will check which of the two checkboxes has been changed, and then gets the value its been changed to, and then displays the message in the status bar. Run the applet to check it out, its pretty cool.

Table Of Contents
5

Text Field Events If you want to get the text thats in a TextField before someone presses a button, you can do that as its changed. You do it with the textValueChanged method:
public void textValueChanged(TextEvent txt) { Object source = txt.getSource(); if (source == name) { showStatus(Name + name.getText()); } else { showStatus(Age + age.getText()); } }

Miscellaneous How to enable and disable components You may have seen some components grayed out so you cant edit them. Well, this is done with the setEditable(boolean) method. For example, this will let someone enable the button named fred: fred.setEditable(true); This will make it so it cannot be edited by users and is grayed out: fred.setEditable(false); Conclusion Well, this is the end of the tutorial. Thank you for taking the time to read this, I hope Ive helped you learn more about GUIs. If you have any questions or comments, feel free to e-mail me at: master_0f_souls@hotmail.com (NOTE: The o in of is a ZERO) Also, please vote for my code at www.planetsourcecode.com , and feel free to give any feedback to there or my email. Thanks!

Table Of Contents
6

You might also like