- GWT Tutorial
- GWT - Home
- GWT - Overview
- GWT - Environment Setup
- GWT - Applications
- GWT - Create Application
- GWT - Deploy Application
- GWT - Style with CSS
- GWT - Basic Widgets
- GWT - Form Widgets
- GWT - Complex widgets
- GWT - Layout Panels
- GWT - Event Handling
- GWT - Custom Widgets
- GWT - UIBinder
- GWT - RPC Communication
- GWT - JUnit Integration
- GWT - Debugging Application
- GWT - Internationalization
- GWT - History Class
- GWT - Bookmark Support
- GWT - Logging Framework
- GWT Useful Resources
- GWT - Questions and Answers
- GWT - Quick Guide
- GWT - Useful Resources
- GWT - Discussion
GWT - CheckBox Widget
Introduction
The Checkbox widget represents a standard checkbox.
Class Declaration
Following is the declaration for com.google.gwt.user.client.ui.CheckBox class −
public class CheckBox extends ButtonBase implements HasName
CSS Style Rules
Following default CSS Style rules will be applied to all the CheckBox widget. You can override it as per your requirements.
.gwt-CheckBox {} .gwt-CheckBox-disabled {}
Class Constructors
Sr.No. | Constructor & Description |
---|---|
1 |
CheckBox() Constructor for CheckkBox. |
2 |
CheckBox(Element element) This constructor may be used by subclasses to explicitly use an existing element. |
3 |
CheckBox(java.lang.String label) Creates a check box with the specified text label. |
4 |
CheckBox(java.lang.String label, boolean asHTML) Creates a check box with the specified text label and set its contents as HTML. |
Class Methods
Sr.No. | Function name & Description |
---|---|
1 |
java.lang.String getName() Gets the widget's name. |
2 |
int getTabIndex() Gets the widget's position in the tab index. |
3 |
java.lang.String getText() Gets this object's text. |
4 |
boolean isChecked() Determines whether this check box is currently checked. |
5 |
boolean isEnabled() Gets whether this widget is enabled. |
6 |
protected void onEnsureDebugId(java.lang.String baseID) Affected Elements: -label = label next to checkbox. |
7 |
protected void onLoad() This method is called when a widget is attached to the browser's document. |
8 |
protected void onUnload() This method is called when a widget is detached from the browser's document. |
9 |
protected void replaceInputElement(Element elem) Replace the current input element with a new one. |
10 |
void setAccessKey(char key) Sets the widget's 'access key'. |
11 |
void setChecked(boolean checked) Checks or unchecks this check box. |
12 |
void setEnabled(boolean enabled) Sets whether this widget is enabled. |
13 |
void setFocus(boolean focused) Explicitly focus/unfocus this widget. |
14 |
void setHTML(java.lang.String html) Sets this object's contents via HTML. |
15 |
void setName(java.lang.String name) Sets the widget's name. |
16 |
void setTabIndex(int index) Sets the widget's position in the tab index. |
17 |
void setText(java.lang.String text) Sets this object's text. |
18 |
void sinkEvents(int eventBitsToAdd) Adds a set of events to be sunk by this object. |
19 |
java.lang.String getHTML() Gets this object's contents as HTML. |
Methods Inherited
This class inherits methods from the following classes −
com.google.gwt.user.client.ui.UIObject
com.google.gwt.user.client.ui.Widget
com.google.gwt.user.client.ui.FocusWidget
java.lang.Object
CheckBox Widget Example
This example will take you through simple steps to show usage of a CheckBox Widget in GWT. Follow the following steps to update the GWT application we created in GWT - Create Application chapter −
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a package com.tutorialspoint as explained in the GWT - Create Application chapter. |
2 | Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorld.html and HelloWorld.java as explained below. Keep rest of the files unchanged. |
3 | Compile and run the application to verify the result of the implemented logic. |
Following is the content of the modified module descriptor src/com.tutorialspoint/HelloWorld.gwt.xml.
<?xml version = "1.0" encoding = "UTF-8"?> <module rename-to = 'helloworld'> <!-- Inherit the core Web Toolkit stuff. --> <inherits name = 'com.google.gwt.user.User'/> <!-- Inherit the default GWT style sheet. --> <inherits name = 'com.google.gwt.user.theme.clean.Clean'/> <!-- Specify the app entry point class. --> <entry-point class = 'com.tutorialspoint.client.HelloWorld'/> <!-- Specify the paths for translatable code --> <source path = 'client'/> <source path = 'shared'/> </module>
Following is the content of the modified Style Sheet file war/HelloWorld.css.
body { text-align: center; font-family: verdana, sans-serif; } h1 { font-size: 2em; font-weight: bold; color: #777777; margin: 40px 0px 70px; text-align: center; } .gwt-CheckBox{ color:green; } .gwt-CheckBox-disabled { color:green; }
Following is the content of the modified HTML host file war/HelloWorld.html.
<html> <head> <title>Hello World</title> <link rel = "stylesheet" href = "HelloWorld.css"/> <script language = "javascript" src = "helloworld/helloworld.nocache.js"> </script> </head> <body> <h1>CheckBox Widget Demonstration</h1> <div id = "gwtContainer"></div> </body> </html>
Let us have following content of Java file src/com.tutorialspoint/HelloWorld.java which will demonstrate use of CheckBox widget.
package com.tutorialspoint.client; import com.google.gwt.core.client.EntryPoint; 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 HelloWorld implements EntryPoint { public void onModuleLoad() { // Make a new check box, and select it by default. CheckBox checkBox1 = new CheckBox("Check Me!"); CheckBox checkBox2 = new CheckBox("Check Me!"); // set check box as selected checkBox1.setValue(true); //disable a checkbox checkBox2.setEnabled(false); checkBox1.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { CheckBox checkBox = (CheckBox)event.getSource(); Window.alert("CheckBox is " + (checkBox.getValue() ? "" : "not") + " checked"); } }); // Add checkboxes to the root panel. VerticalPanel panel = new VerticalPanel(); panel.setSpacing(10); panel.add(checkBox1); panel.add(checkBox2); RootPanel.get("gwtContainer").add(panel); } }
Once you are ready with all the changes done, let us compile and run the application in development mode as we did in GWT - Create Application chapter. If everything is fine with your application, this will produce following result −
When you click Click Me CheckBox, it will show an alert message saying CheckBox is checked or not checked.