Home » Enterprise Java » Vaadin » Netbeans Vaadin Example

About Jesus Boadas

Jesus Boadas
I'm a self taught programmer, I began programming back in 1991 using an IBM A10 mainframe with Pascal an Assembler IBM 360/70 emulator and Turbo C on a X86 PC, since that I work for the banking industry with emerging technologies like Fox Pro, Visual Fox Pro, Visual Basic, Visual C++, Borland C++, lately I moved out to the Airline industry, leading designing and programming in-house web applications with Flex, Actionscript, PHP, Python and Rails and in the last 7 years I focused all my work in Java, working on Linux servers using GlassFish, TomCat, Apache and MySql.

Netbeans Vaadin Example

Netbeans is a well known open source Java IDE, that supports development of all Java application types out of the box. It has an Ant-based project system and supports, Maven, refactoring, version control and has a lot of useful features to help you in your Java development.

1. The tools

  • Java JDK 8
  • Latest Netbeans
  • Vaadin 7.6.6
  • Tomcat Server 8

2. Introduction

You can create Vaadin projects in Netbeans using the plugin. In this example we are going to see how to download and install Netbeans, install the plugin, create a Vaadin project and run it on Tomcat.

3. Prerequisites

  • JDK installed

3. Download NetBeans

Open your browser and go to the page Download Netbeans and download the NetBeans Ide for JavaEE.

1 Download Netbeans

1 Download Netbeans

4. Install NetBeans

When the download is finished, double click on the file. Select the application servers to be installed and hit next.

2 Select servers

2 Select servers

Accept the terms in the license.

3 Accept license

3 Accept license

Choose the destination directory and the JDK. Then press next.

4 Destination directory

4 Destination directory

Choose the directory for the GlassFish application server and press next.

5 GlassFish Directory

5 GlassFish Directory

Choose the directory for the Tomcat server and then press next.

6 Tomcat Directory

6 Tomcat Directory

Review the settings and press install.

7 Review the settings

7 Review the settings

5. Install The Vaadin plugin

Start up NetBeans and from the menu Tools choose Plugins:

8 Install Vaadin plugin

8 Install Vaadin plugin

Select the available plugins tab. From the list choose Vaadin Plug-in for NetBeans and then press install.

9 Plugins Menu

9 Plugins Menu

Review the plugins to install and then press next.

10 Review the plugins

10 Review the plugins

Accept the terms of the license and press install.

11 Plugin license

11 Plugin license

Once the plugin is installed choose restart IDE now and press finish. In order to use the plugin we need to restart the IDE.

12 Restart Netbeans

12 Restart Netbeans

6. Check for updates

From the help menu choose “Check for Updates”

13 Check updates

13 Check updates

When the proccess is done press finish.

14 No updates available

14 No updates available

7. Create the Vaadin project

In the file menu choose New project.

15 New Project

15 New Project

From the categories choose Vaadin and in projects choose “Vaadin Web Application project” and press the next button.

16 Vaadin Project

16 Vaadin Project

Write the name of the project and press finish.

17 Name the project

17 Name the project

Now NetBeans is going to use maven to download all needed dependencies.

8. Review the created project

When NetBeans finish the project creation task, you should get a project structure like the following image:

18 Project structure

18 Project structure

Open the file MyUI.java.

19 MyUI java file

19 MyUI java file

And you can see the code generated by NetBeans. Lets examine the init method

MyUI.java

public class MyUI extends UI {

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout layout = new VerticalLayout();
        
        final TextField name = new TextField();
        name.setCaption("Type your name here:");

        Button button = new Button("Click Me");
        button.addClickListener( e -> {
            layout.addComponent(new Label("Thanks " + name.getValue() + ", it works!"));
        });
        
        layout.addComponents(name, button);
        layout.setMargin(true);
        layout.setSpacing(true);
        
        setContent(layout);
    }
}

final VerticalLayout layout = new VerticalLayout(); Creates a vertical layout.
final TextField name = new TextField(); Creates a text field.
name.setCaption("Type your name here:"); sets the caption of the text field.
Button button = new Button("Click Me"); creates a button.
button.addClickListener adds a click listener.
layout.addComponent(new Label("Thanks " + name.getValue() + ", it works!")); the click listener adds a label every time the button is clicked
layout.addComponents(name, button); adds the text field and the button to the layout.
layout.setMargin(true); sets the margin of the layout.
layout.setSpacing(true); sets the spacing of the layout.
setContent(layout); sets the vertical layout as a main layout.

9. Run the application

Right click on the project and choose run

20 Run project

20 Run project

Choose the server and press OK.

21 Choose server

21 Choose server

The first time you run the project, NetBeans will compile the widgetset and you get an output log like the following.

NetBeans compile widgetset

   Adding '1' new generated units
      [WARN] Warnings in 'C:\devel\Java\JEE\VaadinNetbeans\target\.generated\com\vaadin\client\metadata\ConnectorBundleLoaderImpl.java'
         [WARN] Line 4342: Referencing deprecated class 'com.vaadin.shared.ui.progressindicator.ProgressIndicatorState'
         [WARN] Line 4345: Referencing deprecated class 'com.vaadin.shared.ui.progressindicator.ProgressIndicatorState'
         [WARN] Line 4348: Referencing deprecated class 'com.vaadin.shared.ui.progressindicator.ProgressIndicatorState'
   Computing all possible rebind results for 'com.vaadin.client.ui.dd.VAcceptCriterionFactory'
      Rebinding com.vaadin.client.ui.dd.VAcceptCriterionFactory
         Invoking generator com.vaadin.server.widgetsetutils.AcceptCriteriaFactoryGenerator
            Detecting available criteria ...
            creating mapping for com.vaadin.event.dd.acceptcriteria.AcceptAll
            creating mapping for com.vaadin.event.dd.acceptcriteria.And
            creating mapping for com.vaadin.event.dd.acceptcriteria.ContainsDataFlavor
            creating mapping for com.vaadin.event.dd.acceptcriteria.SourceIs
            creating mapping for com.vaadin.ui.AbstractSelect.TargetItemIs
            creating mapping for com.vaadin.ui.AbstractSelect.AcceptItem
            creating mapping for com.vaadin.ui.Table.TableDropCriterion
            creating mapping for com.vaadin.ui.Tree.TreeDropCriterion
            creating mapping for com.vaadin.event.dd.acceptcriteria.Not
            creating mapping for com.vaadin.event.dd.acceptcriteria.Or
            creating mapping for com.vaadin.event.dd.acceptcriteria.ServerSideCriterion
            creating mapping for com.vaadin.event.dd.acceptcriteria.SourceIsTarget
            creating mapping for com.vaadin.event.dd.acceptcriteria.TargetDetailIs
            creating mapping for com.vaadin.ui.Tree.TargetInSubtree
            Done. (0seconds)
   Compiling 1 permutation
      Compiling permutation 0...
   Compile of permutations succeeded
   Compilation succeeded -- 158,081s
Linking into C:\devel\Java\JEE\VaadinNetbeans\target\classes\VAADIN\widgetsets\com.mycompany.vaadinnetbeans.MyAppWidgetset
   Link succeeded
   Linking succeeded -- 0,889s

If your application compiles whitout errors you get the log.

Tomcat log

=================================================================
Vaadin is running in DEBUG MODE.
Add productionMode=true to web.xml to disable debug features.
To show debug window, add ?debug to your application URL.
=================================================================

10. The results

22 Results

22 Results

We get the text field with the caption, the button and it works as expected.

11. Complete source code

MyUI.java

package com.mycompany.vaadinnetbeans;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

/**
 * This UI is the application entry point. A UI may either represent a browser window 
 * (or tab) or some part of a html page where a Vaadin application is embedded.
 *

* The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be * overridden to add component to the user interface and initialize non-component functionality. */ @Theme(“mytheme”) @Widgetset(“com.mycompany.vaadinnetbeans.MyAppWidgetset”) public class MyUI extends UI { @Override protected void init(VaadinRequest vaadinRequest) { final VerticalLayout layout = new VerticalLayout(); final TextField name = new TextField(); name.setCaption(“Type your name here:”); Button button = new Button(“Click Me”); button.addClickListener( e -> { layout.addComponent(new Label(“Thanks ” + name.getValue() + “, it works!”)); }); layout.addComponents(name, button); layout.setMargin(true); layout.setSpacing(true); setContent(layout); } @WebServlet(urlPatterns = “/*”, name = “MyUIServlet”, asyncSupported = true) @VaadinServletConfiguration(ui = MyUI.class, productionMode = false) public static class MyUIServlet extends VaadinServlet { } }

12. Download the Source Code

This was an example of: Using Vaadin in the NetBeans IDE.

Download
You can download the NetBeans project here: VaadinNetbeans
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of