Introduction to Event Driven Programming in Java
Event-driven programming is the paradigm on which Java GUIs (graphical user interfaces), C# GUIs and Visual Basic GUIs are based. In this tutorial we introduce the basics of event-driven programming in Java. These concepts carry over to the other languages as well. We create a JFrame object, populate it with a button object and a label object, and put code behind the click event of the button.
In Figure 01 we add a JFrame class to an empty project. The project doesn't have to be empty, but for the sake of clarity it is empty in this example.
In Figure 02 we give the JFrame class a name. For this example we simply accept the default name and click Finish to complete the process. The Jframe will be the object that contains our controls. Our controls will trigger events. Our code will run when the events trigger.
Figure 03 illustrates the JFrame class in design view. On the left of the figure is the empty JFrame class. On the right of the figure is the Palette. The Palette provides a menu of controls that can be added to the JFrame. This is much easier than writing code to create controls.
By the way, a great way to learn how to write code to create controls is to use the visual designer, then peek at the code behind the fold. The NetBeans-generated code provides an instant mini-tutorial. Look for the fold icon in the left margin of the Code View.
Next, we add one or more controls to the JFrame. For this example we add a button and a label. Click the button in the Palette, then click the location on the JFrame where the button should be created. NetBeans adds the button to the JFrame and generates necessary the Java code.
The button can be dragged and re-sized after dropping it onto the JFrame. It's not necessary to do so here. We aren't concerned about aesthetics
at this point.
Next, click on the label in the Palette and click on the JFrame to create a label object. The label on the JFrame can be dragged and re sized.
Figure 04 illiustrates what your JFrame might look like after adding the two controls.
Much code has been written for us by NetBeans. We are now ready to add some code of our own. Controls on a JFrame have events that take place as your program runs. Some events are widely used and some are almost obscure. For this tutorial we will focus on the Click Event of the button that we added.
We want our code to run when the button is clicked. That's the purpose of a button control. Therefore, we will put our code into the event handler for the Click Event of the specific button. The Event Handler is simply a method that has been configured to execute when a physical event takes place.
Figure 05 illustrates how to being up the Click Event for the button right-click on the button, select Events / Action / Action Performed.
After creating the Click Event Handler, the Design View will be replaced by the Source View. The Source View is the programming that creates the JFrame. At this point in the process, all the source code has been automagically generated for you by NetBeans. When you added the JFrame class to the project, the code was generated.
Note that an empty method has been created; it's the event handler for the Click Event of the button. That's where our code will go.
Figure 06 displays the Source View and the new event handler.
The key concept is that whatever code we put in the event handler will be executed when the project is executed and the button is clicked. We leverage the event handler method.
In figure 07 we add one line of code to the event handler. The code simply puts the message "Hello World" in the label that we put on the JFrame.
The setText( ) method is a member of the JLabel class.
The program is now ready to run. it doesn't do much, but it does illustrate the concept of event-driven programming in Java.
Figure 08 is the result of running the program and clicking on the button; the contents of the label is changed to "Hello World".
More by this Author
We could nag, but this is better. It is less work for us to use the words of others to emphasize the importance of doing laundry. Besides, if you saw the way we dressed, you'd laugh your mouse off.
Data Hiding is an aspect of Object Oriented Programming (OOP) that allows developers to protect private data and hide implementation details. In this tutorial we examine basic data hiding techniques in Java.
Ever been to a NASCAR race? I thought not. Here are my top 10 reasons why NASCAR racing doesn't rock.