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.
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
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.
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:
Delete the comment shown and enter the following code:
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.
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:
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...
- Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 10
The FOR loop is one of the most common looping controls used in Java. The FOR loop forces the program to repeat a statement or a group of statements a specified number of times. It has three parts; the initial value part, condition part, and ... - Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 11
The WHILE loop in Java execute a statement or a group of statements so long as the specified condition remains TRUE. The loop starts with the keyword “while”. Just after the keyword you open the round brackets and specify the condition to be tested. - Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 29
Being able to code advanced Java classes and being able to manipulate Java methods is one of the hands on requirement skills for Java programmers. This article will discuss how to implement Java classes and how to manipulate Java methods. - Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 18
Array lists in Java are dynamic structures for store of values, that means values can be added or be removed from the list. We do not even need to declare the size of the list but values can be accessed by their index positions. - Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 1
Java is increasingly becoming the world’s programming language of choice. This tutorial will take you on step by step lessons covering specialized topics on Java programming that are aimed at providing the fundamental skills needed for beginner ...