Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Vaadin 7 UI Design By Example: Beginner's Guide

You're reading from   Vaadin 7 UI Design By Example: Beginner's Guide Do it all with Java! All you need is Vaadin and this book which shows you how to develop web applications in a totally hands-on approach. By the end of it you'll have acquired the knack and taken a fun journey on the way.

Arrow left icon
Product type Paperback
Published in Jul 2013
Publisher Packt
ISBN-13 9781782162261
Length 246 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (17) Chapters Close

Vaadin 7 UI Design By Example Beginner's Guide
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
1. Writing Your First Vaadin-powered Application FREE CHAPTER 2. Using Input Components and Forms – Time to Listen to Users 3. Arranging Components into Layouts 4. Using Vaadin Navigation Capabilities 5. Using Tables – Time to Talk to Users 6. Adding More Components 7. Customizing UI Components – Time to Theme it 8. Developing Your Own Components Pop Quiz Answers Index

Time for action – using text fields


Follow these steps to add some fun to our application:

  1. Change your code to add the required text fields just before the line that creates the button instance by adding the highlighted code:

    // ...
    setContent(layout);
            
    final TextField name1 = new TextField("Somebody's name");
    final TextField name2 = new TextField("somebody's name");
    layout.addComponent(name1);
    layout.addComponent(name2);
            
    Button button = new Button("Click Me");
    // ...
  2. Add the business logic in a new method like this:

    public String getFunnyPhrase(String name1, String name2) {
      String[] verbs = new String[] {
        "eats", "melts", "breaks", "washes", "sells"};
    
      String[] bodyParts = new String[] {
        "head", "hands", "waist", "eyes", "elbows"};
    
      return name1 + " " +verbs[(int) (Math.random() * 100 % verbs.length)] + " " +name2 + "'s " +bodyParts[(int) (Math.random() * 100 % verbs.length)];
    }
  3. Change the listener to display the message returned by the business method:

    public void buttonClick(ClickEvent event) {
      String phrase = getFunnyPhrase(
          name1.getValue(), name2.getValue());
      layout.addComponent(new Label(phrase));
    }

What just happened?

We added a couple of text fields to our layout and then implemented a method to get the message to be shown on the labels. As you may have already guessed, the TextField class defines a getValue() method which returns the value which the user has introduced in the field. In this case, it happens to be a String. All input components (that is, components that get input from a user) have a getValue() method, which we can use to know the values introduced on the user interface. The following is a screenshot of the application (it seems that Maria doesn't like Juan too much):

Notifications

Notifications are a common feature in applications. You might need to notify that some event has occurred in your system when the user performs certain action. For example, in a CRUD (create, read, update, and delete) interface, your application should notify the user whether each action has been successfully executed or not.

Vaadin makes it easy for you to show fancy notifications in your web applications. The Notification class has several static methods you can call from any part of your code to show notifications. For simple notifications you can make a call such as:

Notification.show("User created");

This will display a message centered on the page shown as follows:

The message will disappear when the user moves the mouse or presses any key.

Have a go hero – show notifications

In the funny phrases application, it would be nice if the application alerts the user when no names are given instead of showing no-sense phrases. Would you be able to modify the application in order to make it show this alert using the Notification class? Try it!

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Pop quiz – Vaadin fundamentals

There are a couple of concepts you must keep in mind to be proficient with Vaadin. Answer the following questions and test yourself about your current Vaadin level.

Q1. Which method will be automatically called when somebody browses to your application URL?

  1. UI.setContent(Component component).

  2. UI.init(VaadinRequest request).

  3. VerticalLayout.init(VaadinRequest request).

  4. VerticalLayout.addComponent(Component component).

Q2. Suppose you are overriding the init method of your own UI class. How would you set the root of your components tree?

  1. By calling the setContent method of the UI class and passing the root component.

  2. By calling the addComponent method of the UI class and passing the root component.

  3. Actually, an instance of the UI class I'm writing will be the root of the tree.

  4. There's no such a thing as a components tree (it seems the book's author is going crazy).

You have been reading a chapter from
Vaadin 7 UI Design By Example: Beginner's Guide
Published in: Jul 2013
Publisher: Packt
ISBN-13: 9781782162261
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime