Home » Enterprise Java » gwt » RadioButton Example

About Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

RadioButton Example

This is an example of how to create a RadioButton 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 add a radio button example we have followed the steps below:

  • The RadioButtonExample 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 RadioButton.
  • Add a ClickHandler to the radio button and override its onClick(ClickEvent event) method to handle the click events.
  • Add the radio button to the VerticalPanel.
  • Add the VerticalPanel to the RootPanel, that is the panel to which all other widgets must ultimately be added.

Let’s take a look at the code snippet that follows: 

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.RadioButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RadioButtonExample implements EntryPoint {

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

	@Override
	public void onModuleLoad() {

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


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


//Add Item in group 'Items'
			final RadioButton radioButton = new RadioButton("Items", Items[i]);



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



//Set the last radio button disabled by default
			if (i > 4)
				radioButton.setEnabled(false);



//Add radio button to Vertical Panel
			vp.add(radioButton);
		}


  //Add Vertical Panel to Root Panel
		RootPanel.get().add(vp);

	}
	
    // Method that notifies the user which radio button is checked 
	public void check(RadioButton radioButton){
		Window.alert(radioButton.getText() + " is checked");
	}

}

 
This was an example of how to create a RadioButton 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