You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultButtonExample.java 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.tests.book;
  5. import com.vaadin.event.Action;
  6. import com.vaadin.event.ShortcutAction;
  7. import com.vaadin.event.Action.Handler;
  8. import com.vaadin.ui.Button;
  9. import com.vaadin.ui.CustomComponent;
  10. import com.vaadin.ui.FormLayout;
  11. import com.vaadin.ui.HorizontalLayout;
  12. import com.vaadin.ui.Label;
  13. import com.vaadin.ui.Panel;
  14. import com.vaadin.ui.TextField;
  15. public class DefaultButtonExample extends CustomComponent implements Handler {
  16. // Define and create user interface components
  17. Panel panel = new Panel("Login");
  18. FormLayout formlayout = new FormLayout();
  19. TextField username = new TextField("Username");
  20. TextField password = new TextField("Password");
  21. HorizontalLayout buttons = new HorizontalLayout();
  22. // Create buttons and define their listener methods.
  23. Button ok = new Button("OK", this, "okHandler");
  24. Button cancel = new Button("Cancel", this, "cancelHandler");
  25. // Have the unmodified Enter key cause an event
  26. Action action_ok = new ShortcutAction("Default key",
  27. ShortcutAction.KeyCode.ENTER, null);
  28. // Have the C key modified with Alt cause an event
  29. Action action_cancel = new ShortcutAction("Alt+C",
  30. ShortcutAction.KeyCode.C,
  31. new int[] { ShortcutAction.ModifierKey.ALT });
  32. public DefaultButtonExample() {
  33. // Set up the user interface
  34. setCompositionRoot(panel);
  35. panel.addComponent(formlayout);
  36. formlayout.addComponent(username);
  37. formlayout.addComponent(password);
  38. formlayout.addComponent(buttons);
  39. buttons.addComponent(ok);
  40. buttons.addComponent(cancel);
  41. // Set focus to username
  42. username.focus();
  43. // Set this object as the action handler
  44. System.out.println("adding ah");
  45. panel.addActionHandler(this);
  46. System.out.println("start done.");
  47. }
  48. /**
  49. * Retrieve actions for a specific component. This method will be called for
  50. * each object that has a handler; in this example just for login panel. The
  51. * returned action list might as well be static list.
  52. */
  53. public Action[] getActions(Object target, Object sender) {
  54. System.out.println("getActions()");
  55. return new Action[] { action_ok, action_cancel };
  56. }
  57. /**
  58. * Handle actions received from keyboard. This simply directs the actions to
  59. * the same listener methods that are called with ButtonClick events.
  60. */
  61. public void handleAction(Action action, Object sender, Object target) {
  62. if (action == action_ok) {
  63. okHandler();
  64. }
  65. if (action == action_cancel) {
  66. cancelHandler();
  67. }
  68. }
  69. public void okHandler() {
  70. // Do something: report the click
  71. formlayout.addComponent(new Label("OK clicked. " + "User="
  72. + username.getValue() + ", password=" + password.getValue()));
  73. //
  74. }
  75. public void cancelHandler() {
  76. // Do something: report the click
  77. formlayout.addComponent(new Label("Cancel clicked. User="
  78. + username.getValue() + ", password=" + password.getValue()));
  79. }
  80. }