Updated date:

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 38

Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.

Table of Contents

<< Lesson 37 | Lesson 39 >>

Lesson 38: Working with GUI controls: Command Buttons

In the previous lesson, we looked into how we can work with Label and Text Field GUI controls in Java NetBeans. Just to revisit what we covered, we learnt how to drag and drop the controls onto a form from the Palette window and how to apply some of the properties.

We also noted that it is very important to name controls with meaningful names which makes it easy to reference them in the program when coding. Open the form we had in the previous lesson, it should have a Label and a Text field already as we created them. In this lesson, we shall learn how to use Command Button GUI control.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-38

Command Button GUI control

Command button is one of the most used GUI control in any visual programming environment. It is a control that is usually clicked to perform an action. It can contain an image or a text caption representing the action to be performed.

We’ll add a Command button onto the form and then add some code for it so that when it is clicked, it fills the Text field with the text “Manipulating Java Netbeans GUI controls.”

Add a Command button from the Palette window under the Swing controls by dragging and dropping onto the form.

Palette window

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-38
programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-38

Change the text caption of the Command button to “Show text” by right-clicking on it and choosing “Edit Text” and then right-click again, this time choose “Change Variable Name” and change its name from the default “jButton1” to “CmdText” to reference it with a meaningful name.

Changing variable name

Changing variable name

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-38

Now our Command button is almost set but it lacks the code to enable display of text on the Text field. To add the code, double-click the Command button. Double clicking the command button will reveal the source code and activate the event associated with the Command button when it is clicked. Locate the ActionPerformed event code as shown below:

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-38

Delete the comment shown and enter the following code:

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-38

Since we want to display a particular text in the Text field when the Command button is clicked, we have to put this code in the ActionPerformed event method of the Command button. The code uses a method of Text field called setText() that displays the text contained inside the double quotes.

Notice how we reference the Text field with its variable name “txtShow” the keyword “this” signifies that we are dealing with the current form.

Run the form and you should be able to see the text being displayed on the Text field when you click the Command button.

Note: You may need to adjust the size of the form and Text field if it is not enough for the text.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-38

ActionPerformed event is just one of the many methods included for Command button. You should explore others like focusGained, mouseEntered, mouseExited, mouseReleased etc. Each event method works differently depending on which action is triggered, for example, if we want to change the text caption on the Command button from “Show text” to “Click me!” at run time when the mouse is placed on the Command button and change it back to “Show text” when the mouse is off the Command button, we’ll use the mouseEntered and mouseExited events as shown:

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-38
programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-38

As you can see, some controls such as Command button will do nothing unless some code is defined for their action. Event methods available for command button can be used to manipulate many other controls as we’ll learn later. In the next lesson, we shall cover Combo Box GUI control, see you then.

<< Lesson 37 | Lesson 39 >>

Other related Hubs...

Want to make money online?

Related Articles