Table of contents
1.
Introduction
2.
ToggleButton Class
3.
CSS Style Rules
4.
Constructors 
5.
Methods
6.
GWT ToggleButton Widget Example
6.1.
Step 1: Project Creation👨‍💻
6.2.
Step 2: Modification in Files👨‍🔧
6.3.
Step 3: Check the Result🤳🏼
6.4.
Output
7.
Frequently Asked Questions
7.1.
What are the GWT system prerequisites?
7.2.
Is GWT is too expensive?
7.3.
Can I create an enterprise or commercial application with GWT?
7.4.
Which browsers is GWT compatible with?
7.5.
Is GWT offered in India? Does it translate into my language?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

GWT ToggleButton Widget

Author Aditya Kumar
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A development toolkit for making RICH Internet Applications(RIA) is called Google Web Toolkit (GWT). It gives programmers the option to create client-side Java applications. The Java code is converted to JavaScript code via GWT.

An attractive stateful button that enables the user to switch between up and down states is represented by the ToggleButton widget.

The user can switch between two states of a setting using a GWT ToggleButton Widget.

GWT ToggleButton Widget

ToggleButton Class

The ToggleButton class in the GWT (Google Web Toolkit) ToggleButton widget, is declared as follows:

public class ToggleButton extends CustomButton
You can also try this code with Online Java Compiler
Run Code


The following classes' methods are inherited by the ToggleButton class:

Methods in toggleButton Class

The ToggleButton class's implemented interfaces are listed below:

  • IsEditor<LeafValueEditor<java.lang.Boolean>>
  • HasAllDragAndDropHandlers
  • HasAllFocusHandlers
  • HasAllGestureHandlers
  • HasAllKeyHandlers
  • HasAllMouseHandlers
  • HasAllTouchHandlers
  • HasBlurHandlers
  • HasClickHandlers, etc.

CSS Style Rules

The GWT PushButton widgets shown below will all be styled using the standard CSS(Cascading Style Sheets) guidelines. Following your needs, you can override it.

.gwt-ToggleButton-up {}

.gwt-ToggleButton-down {}

.gwt-ToggleButton-up-hovering {}

.gwt-ToggleButton-down-hovering {}

.gwt-ToggleButton-up-disabled {}

.gwt-ToggleButton-down-disabled {} 

Constructors 

In the GWT ToggleButton Widget, the following are the constructors mentioned below:

Sr. No. Constructor  Description
1. ToggleButton() ToggleButton Constructor
2. ToggleButton(Image upImg) A ToggleButton with an upstate image is created.
3. ToggleButton(Image upImg, ClickListener listener) Creates a ToggleButton with a ClickListener and an upstate picture.
4. ToggleButton(Image upImg, Image downImage) A ToggleButton with an upstate image is created.
5. ToggleButton(Image upImg, Image downImg, ClickListener listener) A ToggleButton with an upstate image is created.
6. ToggleButton(java.lang.String upTxt) A ToggleButton with an upstate text is created.
7. ToggleButton(java.lang.String upTxt, ClickListener listener) Creates a ToggleButton with a ClickListener and an upstate text.
8. ToggleButton(java.lang.String upTxt, java.lang.String downText) Makes a ToggleButton with text for the up and down states.
9. ToggleButton(java.lang.String upTxt, java.lang.String downTxt, ClickListener listener) Creates a ToggleButton with an upstate text, a downstate text, and a click listener.

 

Methods

In the GWT ToggleButton Widget, the following are the methods mentioned below:

Sr. No. Function Description
1. boolean isDown()

This will check whether the button is down or not.

Overrides: isDown in class CustomButton

If the button is pressed or down, it returns true.

2. protected void onClick()

When the user has finished selecting this button, a call is made. The click event is fired to listeners by default. Overriding subclasses of CustomButton This method should be overridden by onClickStart() to bring back the default widget display.

Overrides: OnClick in class CustomButton

3. void setDown(boolean down)

Whether or not this button is pressed or down. notifies ValueChangeEvent of changes. (Use setValue(Boolean, boolean) if you want the event to fire.)

SetDown is overridden in the class CustomButton.

4.  void setValue(java.lang.Boolean value) Whether or not this button is pressed or down.
5.  void setValue(java.lang.Boolean value, boolean fireEvents) Sets if the button is pressed down and, if necessary, triggers a ValueChangeEvent.

 

GWT ToggleButton Widget Example

This example will walk you through some straightforward GWT ToggleButton Widget usage instructions. To upgrade the GWT application we developed in GWT, follow these steps:

Step 1: Project Creation👨‍💻

Create a project with the name GWTToggleButton.

Step 2: Modification in Files👨‍🔧

Modify the GWTToggleButton.gwt.xml, GWTToggleButton.css, GWTToggleButton.html, and GWTToggleButton.java files as explained below. Keep the rest of the files unchanged.

Step 3: Check the Result🤳🏼

Compile and run the application to check your results.

The changed module description, located at ‘src/com.codingninjas.gwttogglebut/GWTToggleButton.gwt.xml’, contains the content given below:

<? xml version = "1.0" encoding = "UTF-8" ?>
  <module rename-to='gwttooglebutton'>
    <inherits name='com.google.gwt.user.User' />
    <!-- for default stylesheet, you can add any style to your project-->
    <!-- <inherits name='com.google.gwt.user.theme.clean.Clean' /> -->

    <entry-point class='com.codingninjas.gwttogglebut.client.GWTToggleButton' />
    <source path='client' />
    <source path='shared' />
    <!-- allow Super Dev Mode -->
    <add-linker name="xsiframe" />
  </module>


Now write some styling in the CSS file for your project which is located at ‘war/GWTToggleButton.css’.

body {
  text - align: center;
  font - family: verdana, sans - serif;
}

h1 {
  font - size: 3em;
  font - weight: bold;
  color: #004080;
  margin: 30px 0px 80px;
  text - align: center;
}

.gwt - ToggleButton {
  color: #32CD32;
}

.gwt - ToggleButton - up {
  color: #5e17eb;
}

.gwt - ToggleButton - down {
  color: purple;
}

.gwt - ToggleButton - up - hovering {
  color: yellow;
}

.gwt - ToggleButton - down - hovering {
  color: blue;
}

.gwt - ToggleButton - up - disabled {
  color: violet;
}

.gwt - ToggleButton - down - disabled {
  color: red;
}


Now write in the HTML host file for your project which is located at ‘war/GWTToggleButton.html’.

<html>
  <head>
    <title>Coding Ninja’s ToggleButton Example</title>
    <link rel="stylesheet" href="GWTToggleButton.css" />
    <script language="javascript" src="gwttogglebutton/gwttogglebutton.nocache.js">
    </script>
  </head>

  <body>
    <h1>ToggleButton Example</h1>
    <div id="handleDiv"></div>
  </body>
</html>


The Java file ‘src/com.codingninjas.gwtpushbut/GWTToggleButton.java’ with the following contents will show how to utilize the ToggleButton widget.

package com.codingninjas.gwttogglebut.client;
 
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.Window;
 
public class GWTPushButton implements EntryPoint {
    public void onModuleLoad() {
    	// Making a toggleButton
    	ToggleButton toggleBut1 = new ToggleButton("ToggleButton");
    	
    	// Making another toggleButton
    	ToggleButton toggleBut = new ToggleButton("ToggleButton");
    	
    	// Make disable toggleButton1
    	toggleBut1.setEnabled(false);
    	
    	// Adding onclick event listener
    	toggleBut.addClickHandler(new ClickHandler() {
    		@Override
    		public void onClick(ClickEvent event) {
    			Window.alert("You have clicked on ToggleButton!");
    		}
    	});
    	
    	// Set toggleButtons to RootPanel
    	VerticalPanel panel = new VerticalPanel();
    	panel.setSpacing(10);
    	panel.add(toggleBut);
    	panel.add(toggleBut1);
    	RootPanel.get("handleDiv").add(panel);
    	}   
 }
You can also try this code with Online Java Compiler
Run Code


When you are finished and have made all the necessary modifications, compile and run the application in development mode. 

Output

An alert box containing the text "You have clicked on ToggleButton!" will appear when you choose the "ToggleButton" button.

ToggleButton Output

Frequently Asked Questions

What are the GWT system prerequisites?

JRE(Java Runtime Environment1.5) and Java 2, Operating systems: Mac OS X 10.4+ (Tiger or Leopard), Linux with GTK+ 2.2.1+, Windows Vista/XP/2000,

Hardware: 512MB RAM and 100MB of free disc space.

Is GWT is too expensive?

No, GWT is totally cost-free. In actuality, the Apache 2.0 open source license applies to the entire GWT source code.

Can I create an enterprise or commercial application with GWT?

Yes, GWT can be used for enterprise, commercial, and non-commercial applications. The Apache 2.0 open source license governs the whole GWT source code.

Which browsers is GWT compatible with?

Firefox, Internet Explorer(8, 9, 10, 11), Safari 5, and Chromium.

Is GWT offered in India? Does it translate into my language?

GWT is accessible worldwide and ought to operate in the majority of languages, despite the fact that the documentation is currently only available in American English.

Conclusion

In this article, we have discussed the GWT ToggleButton Widget. We have discussed its constructors, methods, and implementation. GWT ToggleButton Widget is a part of form widgets that are used in many places while developing applications.

We hope this blog has helped you enhance your knowledge regarding GWT ToggleButton Widget. To learn more, check out our articles on GWT RadioButton WidgetGWT CheckBox Widget, and GWT SuggesttBox.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; look at the Top 150 Interview Puzzles interview experiences, and interview bundle for placement preparations.

Thank you

Do upvote our blog to help other ninjas grow. 

Happy Learning Ninja!

Live masterclass