Login Manager Tutorial java problem

GWT Designer allows you to quickly create the modules, composites, panels, remote services and other elements that comprise Google Web Tookit applications.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

Login Manager Tutorial java problem

Postby ewinb » Tue May 11, 2010 2:01 pm

I am working my way through the Login Manager tutorial at
http://download.instantiations.com/D2WB ... nager.html

When I get the part where the onClick is modified to test the textBoxUsername and Password fields, as given in the tutorial, I am getting a java error:
Cannot refer to a non-final variable textBoxUsername inside an inner class defined in a different method. I get a similar error on the textBoxPassword field as well


Here is my full code:

====================================
Code: Select all
package com.gwt.tutorial.LoginManager.client;

import com.google.gwt.user.client.ui.Composite;

public class Login extends Composite {
   private Label lblUsername;
   private Label lblPassword;

   public Login() {
      
      VerticalPanel verticalPanel = new VerticalPanel();
      initWidget(verticalPanel);
      
      Label lbSignInTo = new Label("Sign In");
      verticalPanel.add(lbSignInTo);
      
      FlexTable flexTable = new FlexTable();
      verticalPanel.add(flexTable);
      
      lblUsername = new Label("Username:");
      flexTable.setWidget(0, 0, lblUsername);
      
      TextBox textBoxUsername = new TextBox();
      flexTable.setWidget(0, 1, textBoxUsername);
      
      lblPassword = new Label("Password:");
      flexTable.setWidget(1, 0, lblPassword);
      
      TextBox textBoxPassword = new TextBox();
      flexTable.setWidget(1, 1, textBoxPassword);
      
      CheckBox chckbxRememberMeOn = new CheckBox("Remember me on this computer.");
      flexTable.setWidget(2, 1, chckbxRememberMeOn);
      
      Button btnSigninbutton = new Button("SigninButton");
      btnSigninbutton.addClickHandler(new ClickHandler() {

         @Override
         public void onClick(ClickEvent event) {
            // TODO Auto-generated method stub
            if (textBoxUsername.getText().length() == 0
                  || textBoxPassword.getText().length() == 0) {
                  Window.alert("Username or password is empty.");
               }
         }

      });
      btnSigninbutton.setText("Sign in");
      flexTable.setWidget(3, 1, btnSigninbutton);
   }

}

===================================================

Where have I screwed up?

--EB
ewinb
 
Posts: 8
Joined: Tue May 11, 2010 1:56 pm

Re: Login Manager Tutorial java problem

Postby Konstantin.Scheglov » Tue May 11, 2010 11:52 pm

Depending on your code generation settings components are created as fields or local variables.
If you use local variables, you have to add "final" modifier to them to reference them from anonymous class.
Press Ctrl+1 (quick fix) and Eclipse will suggest to do this for you.
Konstantin.Scheglov
Moderator
 
Posts: 186
Joined: Tue Oct 18, 2005 8:11 pm
Location: Russian Federation, Lipetsk

Re: Login Manager Tutorial java problem

Postby Eric Clayberg » Wed May 12, 2010 4:33 am

Note that you can also easily just create them as fields to begin with.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Re: Login Manager Tutorial java problem

Postby ewinb » Wed May 12, 2010 11:17 am

Thanks, and I have just located the right dialog to "Declare variable as final".

Next question: which is best? Or where in the online doc can I read your prepared answer?

-- Ewin
ewinb
 
Posts: 8
Joined: Tue May 11, 2010 1:56 pm

Re: Login Manager Tutorial java problem

Postby Eric Clayberg » Wed May 12, 2010 1:28 pm

"Which is best" is entirely up to you and the needs of your app. GWT Designer doesn't care one way or the other.
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA


Return to GWT Designer

Who is online

Users browsing this forum: No registered users and 1 guest