Introduction to Java :Components Part-1

Static Text with Label

The Label class gives you the ability to draw a single line of text in your application. There is not much to it, consider the following example:

In all of the following examples, we will use the add() method to add components to a Frame object in order to show them on the monitor. Frame and add() will be discussed in the next section. So don’t worry about the specifics of baseFrame or add() for now. Instead, watch how the components are used within it!

The previous code yields the following interface:

Text Entry Components

The JDK also provides the TextField and TextArea components which allow you to get user input. They are derived from TextComponent, so if you are looking for a method that should obviously be there, you should check TextComponent right away.

When compiled and executed, the code above produces the following interface:

Check Boxes and Check Box Groups

The Check Box and Check Box Groups are very familiar components which allow a user to make a choice between several options. The Check Box component is used to give the user the ability to select or unselect some choice. The Check Box group allows the developer to group a set of check boxes in such a way that only one check box can be selected at any one time (On day one, we saw this type of widget in HTML FORM programming under the name “RADIO”). In the following example we create both single check boxes and checkboxes in a group. Notice that you can select or unselect AFC or JDK without affecting any other check boxes. However, you can choose only male or female from the sex group.


One of the problems with Check Boxes and Check Box Groups is that they take up a lot of space in your interface. If you have a lot of choices, it may be better to present them in a Choice control (similar to the SELECT widget in HTML FORM programming)