Dec 31, 2010

A simple vaadin application

This is a simple vaadin application which get 2 values and give it's sum.

To create this you need Eclipse IDE. (or you can use maven archetype:generate.).
You must install maven-eclipse and vaadin-eclipse plugins.

Then create a new project using maven. You can do it by creating a vaadin project also. But if you use maven, it will be very helpful.
This will create the project and there will be a sample project, but if you view the source code it will prompt some errors. Don't worry just run it once. When it is running, it will download all relevant library files including vaadin.jar.

Now you can add a new class file and insert this coding.

package org.thilina.vaadin;

import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Layout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.*;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Window.Notification;


/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class MyVaadinApplication extends Application {

    private Window window;
    private TextField tf,tf2;
    private Double a,b,sum;
    private Button ok,reset;
    private Notification notification;

  
    @Override
    public void init() {
        window = new Window("My Vaadin Application");
        setMainWindow(window);
        createForm();
    }

    private void createForm() {
        Form form = new Form();
        form.setCaption("Form Caption");
        tf = new TextField("Value 1");
        form.addField("value_1",tf);
      
        tf2 = new TextField("Value 2");
        form.addField("value_2",tf2);

        form.setFooter(new HorizontalLayout());
        Layout okbar = form.getFooter();
        ok=new Button("OK", this, "commit");
        okbar.addComponent(ok);
        reset=new Button("Reset", this, "reset");
        okbar.addComponent(reset);
        window.addComponent(form);
    }

    public void commit(Button.ClickEvent event){
        String temp;
        temp=tf.getValue().toString();
        a=Double.parseDouble(temp);
        temp=tf2.getValue().toString();
        b=Double.parseDouble(temp);
        sum=a+b;
        notification = new Notification("Answer is : "+sum,Notification.TYPE_WARNING_MESSAGE);
        window.showNotification(notification);
    }

    public void reset(Button.ClickEvent event){
    tf.setValue("");
    tf2.setValue("");
    }
}


You need to change the package details in the first row. Then check the class name and file name.

Then you can remove auto generated file.

There are sample source code in the vaadin website. http://demo.vaadin.com/sampler/
Select a part and in that window there is a description about it. Next to that there is a small link to view the source code of the selected item.

2 comments:

  1. Check this out.

    https://github.com/thilinapiy/vaadinlogin-example

    ReplyDelete
  2. Very useful.. thank you so much...

    ReplyDelete

Your comments are always welcome ...