Home » Enterprise Java » gwt » CheckBox Example

About Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

CheckBox Example

In this example we shall show you how to create a CheckBox example using the Google Web Toolkit, that is an open source set of tools that allows web developers to create and maintain complex JavaScript front-end applications in Java. To create a CheckBox example one should perform the following steps:

  • The CheckBoxExample class implements the com.google.gwt.core.client.EntryPoint interface to allow the class to act as a module entry point. It overrides its onModuleLoad() method.
  • Create a new VerticalPanel.
  • Create a few instances of CheckBox.
  • Add a ClickHandler to the check box and override its onClick(ClickEvent event) method to handle the click events.
  • Add the check box to the VerticalPanel.
  • Add the VerticalPanel to the RootPanel, that is the panel to which all other widgets must ultimately be added,

as described in the code snippet below. 

package com.javacodegeeks.snippets.enterprise;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class CheckBoxExample implements EntryPoint {

	final String[] Items = { "Item0", "Item1", "Item2", "Item3", "Item4", "Item5" };

	@Override
	public void onModuleLoad() {

  // Create new Instance of vertical panel to align the check boxes
		VerticalPanel vp = new VerticalPanel();

  // Create i Instances of CheckBox()
		for (int i = 0; i < Items.length; i++) {

// Add Item
			final CheckBox checkBox = new CheckBox(Items[i]);

// Add ClickHandler
			checkBox.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					check(checkBox);
				}
			});

//Set some checkBoxes disabled by default 
			if (i > 3)
				checkBox.setEnabled(false);

//Add checkBox to Vertical Panel
			vp.add(checkBox);
		}
  //Add Vertical Panel to Root Panel
		RootPanel.get().add(vp);
	}
	
    // Method that notifies the user whether a checkBox is checked or not 
	public void check(CheckBox checkBox){
		boolean checked = checkBox.getValue();
		Window.alert(checkBox.getText() + " is " + (checked ? "" : "not ") + "checked");
	}
}

 
This was an example of how to create a CheckBox example using the Google Web Toolkit.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments