Java/GWT/TextBox

Материал из Java эксперт
Перейти к: навигация, поиск

Read only TextBox

package com.jexp.gwt.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.TextBox;
public class GWTClient implements EntryPoint {
  public void onModuleLoad() {
    TextBox readOnlyTextBox = new TextBox();
    readOnlyTextBox.setReadOnly(true);
    readOnlyTextBox.setText("read only");
    RootPanel.get().add(readOnlyTextBox);
  }
}





TextBox ClickListener

package com.jexp.gwt.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HTML;
public class GWTClient implements EntryPoint {
  public void onModuleLoad() {
    final TextBox textBox = new TextBox();
    
    HorizontalPanel p = new HorizontalPanel();
    p.setSpacing(4);
    textBox.setWidth("20em");
    p.add(textBox);
    final HTML echo = new HTML();
  
    p.add(echo);
    textBox.addClickListener(new ClickListener() {
        public void onClick(Widget sender) {
          updateText(textBox, echo);
        }
    });
  
    updateText(textBox, echo);

    RootPanel.get().add(p);
  }
  
  private void updateText(TextBoxBase text, HTML echo) {
    echo.setHTML("Selection: " + text.getCursorPos() + ", "
        + text.getSelectionLength());
  }
  
}





TextBox KeyListener

package com.jexp.gwt.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class GWTClient implements EntryPoint{
  Button button = new Button("Login");
  public void onModuleLoad() {
    final Label loginPrompt = new Label("Please Log In");
    final Grid grid = new Grid(3, 2);
    final Label namePrompt = new Label("Name");
    final TextBox nameTextbox = new TextBox();
    final Label passwordPrompt = new Label("Password:");
    final PasswordTextBox passwordTextbox =
        new PasswordTextBox();
    RootPanel.get().clear();
    loginPrompt.addStyleName("loginPrompt");
    nameTextbox.addStyleName("nameField");
    passwordTextbox.addStyleName("passwordField");
    button.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        Window.alert("Button clicked");
      }
    });
    SubmitListener sl = new SubmitListener();
    passwordTextbox.addKeyboardListener(sl);
    nameTextbox.addKeyboardListener(sl);
    grid.setWidget(0, 0, namePrompt);
    grid.setWidget(0, 1, nameTextbox);
    grid.setWidget(1, 0, passwordPrompt);
    grid.setWidget(1, 1, passwordTextbox);
    grid.setWidget(2, 1, button);
    RootPanel.get().add(loginPrompt);
    RootPanel.get().add(grid);
  }
  private class SubmitListener extends KeyboardListenerAdapter {
    public void onKeyPress(Widget sender, char key, int mods) {
      if (KeyboardListener.KEY_ENTER == key)
        button.click();
    }
  }
}