GWT - FocusPanel Widget



The FocusPanel widget represents a simple panel that makes its contents focusable, and adds the ability to catch mouse and keyboard events.

Class declaration

Following is the declaration for class:

public class FocusPanel
   extends SimplePanel
      implements HasFocus, SourcesClickEvents, 
         SourcesMouseEvents, SourcesMouseWheelEvents, 
            HasAllMouseHandlers, HasClickHandlers, 
               HasDoubleClickHandlers, HasAllKeyHandlers,

Class constructors

S.N.Constructor & Description
Creates an empty focus panel.
2FocusPanel(Widget child)
Creates a new focus panel with the given child widget.

Class methods

S.N.Function name & Description
1HandlerRegistration addBlurHandler(BlurHandler handler)
Adds a BlurEvent handler.
2HandlerRegistration addClickHandler(ClickHandler handler)
Adds a ClickEvent handler.
3void addClickListener(ClickListener listener)
Deprecated. Use addClickHandler( instead
4HandlerRegistration addDoubleClickHandler(DoubleClickHandler handler)
Adds a DoubleClickEvent handler.
5HandlerRegistration addFocusHandler(FocusHandler handler)
Adds a FocusEvent handler.
6void addFocusListener(FocusListener listener)
Deprecated. Use addFocusHandler( instead
7void addKeyboardListener(KeyboardListener listener)
Deprecated. Use addKeyDownHandler(, addKeyUpHandler( and addKeyPressHandler( instead
8HandlerRegistration addKeyDownHandler(KeyDownHandler handler)
Adds a KeyDownEvent handler.
9HandlerRegistration addKeyPressHandler(KeyPressHandler handler)
Adds a KeyPressEvent handler.
10HandlerRegistration addKeyUpHandler(KeyUpHandler handler)
Adds a KeyUpEvent handler.
11HandlerRegistration addMouseDownHandler(MouseDownHandler handler)
Adds a MouseDownEvent handler.
12void addMouseListener(MouseListener listener)
Deprecated. Use addMouseOverHandler(, addMouseMoveHandler(, addMouseDownHandler(, addMouseUpHandler( and addMouseOutHandler( instead
13HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler)
Adds a MouseMoveEvent handler.
14HandlerRegistration addMouseOutHandler(MouseOutHandler handler)
Adds a MouseOutEvent handler.
15HandlerRegistration addMouseOverHandler(MouseOverHandler handler)
Adds a MouseOverEvent handler.
16HandlerRegistration addMouseUpHandler(MouseUpHandler handler)
Adds a MouseUpEvent handler.
17HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler)
Adds a MouseWheelEvent handler.
18void addMouseWheelListener(MouseWheelListener listener)
Deprecated. Use addMouseWheelHandler( instead
19int getTabIndex()
Gets the widget's position in the tab index.
20void removeClickListener(ClickListener listener)
Deprecated. Use the HandlerRegistration.removeHandler() method on the object returned by addClickHandler( instead
21void removeFocusListener(FocusListener listener)
Deprecated. Use the HandlerRegistration.removeHandler() method on the object returned by addFocusHandler( instead
22void removeKeyboardListener(KeyboardListener listener)
Deprecated. Use the HandlerRegistration.removeHandler() method on the object returned by an add*Handler method instead
23void removeMouseListener(MouseListener listener)
Deprecated. Use the HandlerRegistration.removeHandler() method on the object returned by an add*Handler method instead
24void removeMouseWheelListener(MouseWheelListener listener)
Deprecated. Use the HandlerRegistration.removeHandler() method on the object returned by addMouseWheelHandler( instead
25void setAccessKey(char key)
Sets the widget's 'access key'.
26void setFocus(boolean focused)
Explicitly focus/unfocus this widget.
27void setTabIndex(int index)
Sets the widget's position in the tab index.

Methods inherited

This class inherits methods from the following classes:





  • java.lang.Object

FocusPanel Widget Example

This example will take you through simple steps to show usage of a FocusPanel Widget in GWT. Follow the following steps to update the GWT application we created in GWT - Create Application chapter:

1Create a project with a name HelloWorld under a package com.tutorialspoint as explained in the GWT - Create Application chapter.
2Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorld.html and as explained below. Keep rest of the files unchanged.
3Compile 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=''/>

  <!-- Inherit the default GWT style sheet.                       -->
  <inherits name=''/>

  <!-- 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'/>


Following is the content of the modified Style Sheet file war/HelloWorld.css.

   text-align: center;
   font-family: verdana, sans-serif;
   font-size: 2em;
   font-weight: bold;
   color: #777777;
   margin: 40px 0px 70px;
   text-align: center;

Following is the content of the modified HTML host file war/HelloWorld.html.

<title>Hello World</title>
   <link rel="stylesheet" href="HelloWorld.css"/>
   <script language="javascript" src="helloworld/helloworld.nocache.js">

<h1>FocusPanel Widget Demonstration</h1>
<div id="gwtContainer"></div>


Let us have following content of Java file src/com.tutorialspoint/ which will demonstrate use of FocusPanel widget.

package com.tutorialspoint.client;


public class HelloWorld implements EntryPoint {

   public void onModuleLoad() {
      // Create text 
      HTML contents = new HTML("This is a FocusPanel."
      +" Click on the panel and it will attain focus.");

      //create focus panel with content
      FocusPanel focusPanel = new FocusPanel(contents);
      focusPanel.setSize("400px", "100px");

      DecoratorPanel decoratorPanel = new DecoratorPanel();


      // Add the widgets to the root 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:

GWT FocusPanel Widget