ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

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

Updated on October 16, 2019
dwachira profile image

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

Lesson 40: Working with GUI controls: Check box and Text area

Welcome to Lesson 39 of Programming in Java NetBeans, we’ll continue learning more about the Graphical User Interface (GUI) controls provided in Java programming language and specifically in Java NetBeans. In this lesson, we shall discuss Check box GUI control.

In the previous lesson, we did learn about Combo box, a GUI control that allow user to select an item from a drop-down list. Selecting items rather than typing quickens the process of data entry and Java NetBeans provides a number of Controls that enhance user selection of items.

Source

Check box GUI control

A Check box is a control that allows users to choose items by way of selecting or deselecting a tick box provided in a Check box control. A group of Check boxes, unlike Radio Buttons, can allow users to select more than one item.

The best method of working with a group of Check boxes is to put them on a Panel control, that way one can manipulate them as a group rather than individually. A Panel is a Swing container that can hold other controls.

Open the form we have been using since Lesson 36. On this form, we’ll add a sizable Panel that will contain the group of Check boxes. Drag the Panel from the Palette window under the Swing containers and drop it on the form.

Palette window

We are going to have a group of five Check boxes each Check box representing an international language. We’ll re-name the Check boxes variable names to ChkBox1 through ChkBox5 and then change the caption of each Check box from the default “jCheckBox1” to a specific language, for example English. See below:

The group of Check boxes is almost done but don't you think it would be better if we let the user know what he or she is expected to do with the languages.

We’ll add a titled border for the Panel control with a title “Select language” so that the user can select preferred language or languages.

Click on the Panel itself so that it is selected, this will ensure that only the properties that apply to the Panel are active on the Properties window.

On the Properties window, click on border properties and from the dialog window that will popup, select “Titled Border”, enter title as “Select language” and choose and other properties such as font.

Here is the complete and run form with two of the languages selected:

Text area GUI control

We’ll add a Text area control on the form so that when the user selects a language or languages they will be listed on the Text area. Drag and drop a Text area control onto the form from the Palette window under the Swing controls. We’ll re-name this Text area as “TxtLanguages” since it will contain languages.

The form should look as shown when run with two languages still selected. Don’t worry about the scroll bar at the bottom of the Text area. The scroll bar will only appear if the Text area is smaller enough.

We need to know which Check box was selected so that we can add its caption text into the Text area as the selected language. We’ll use an IF statement.

For us to drop the selected language into the Text area, we’ll use a command button which will contain the code with IF statement.

Add a command button into the form as shown below, you might need to re-size the form and the Text area for the controls to fit as shown.

Change the caption of the command button to “Add” since we want the button to add whichever language was selected into the Text area.

The Command button will contain the code with the IF statement. Java Check boxes have a property called isSelected() that return a Boolean true if the Check box is selected or a Boolean false if the Check box is not selected. We can use this property in a series of IF Statements to see if each Check box is selected or not. If they are, we can build up a string, adding the text caption from each Check box. Double click the Command button to enter the following code.

We have set a string variable called lg. If a Check box is selected then we get the caption text of that Check box. This text is then stored in the lg variable, along with a new line character ( '\n')

The last line of code sets the text for the Text area and we are passing this string of text inside the round bracket of setText() method. Running the form now you should have the button adding the language or languages selected into the Text area.

As you can see, the Command button can be able to add the selected language or languages into the Text area. Every time it is clicked the series of IF statements will be executed to reflect the current selected Check boxes. In the next lesson, we shall discuss Radio button GUI control.

<< Lesson 39 | Lesson 41 >>

Comments

    0 of 8192 characters used
    Post Comment
    • dwachira profile imageAUTHOR

      Danson Wachira 

      7 years ago from Nairobi, Kenya

      Hi yoesef,

      Thanks for the visit and comment, i have just added Lesson 41, please check.

    • profile image

      yoesef 

      7 years ago

      where is lesson 41?

    • profile image

      Max 

      7 years ago

      This tutorial is great, just what i needed for my classes, thanks man! May you write more articles on this subject.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://maven.io/company/pages/privacy

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
    ClickscoThis is a data management platform studying reader behavior (Privacy Policy)