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.

Figure 01 - Add a JFrame class to our example project
Figure 01 - Add a JFrame class to our example project

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 02 - Accept the default name for the JFrame
Figure 02 - Accept the default name for the JFrame

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.

Figure 03 - The JFrame and the Palette. This is called the "Design View"
Figure 03 - The JFrame and the Palette. This is called the "Design 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.

Figure 04 - The JFrame, after adding the button and label controls
Figure 04 - The JFrame, after adding the button and label 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.

Figure 05 - Creating the Click Event handler for the button on the JFrame
Figure 05 - Creating the Click Event handler for the button on the JFrame

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.

Figure 06 - The Code View and the newly created Click Event handler
Figure 06 - The Code View and the newly created Click 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.

Figure 07 - Add code to the Event Handler
Figure 07 - Add code to the Event Handler

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".

Figure 08 - The result of running the program and clicking the button
Figure 08 - The result of running the program and clicking the button

More by this Author


Comments 3 comments

klanguedoc profile image

klanguedoc 4 years ago from Canada

Great hub, good advice.


Java Programs profile image

Java Programs 4 years ago from India

Great hub ..... will make so many willing programmer to learn a lot .... thanks


IntegrityYes 4 years ago

I voted up for sure. That is informative.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working