Adding a second class to my maven application

Hi guys,
this is my first application (maven project) using vaadin, so I’m a little lost.
Basically, so far I have one class, MyUI.java like this one

[code]
package my.vaadin.apptest;

import com.vaadin.ui.AbstractJavaScriptComponent;
import elemental.json.JsonObject;
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.Button.ClickEvent;
import com.vaadin.ui.JavaScript;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.client.ui.*;

/**

  • 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(“my.vaadin.apptest.MyAppWidgetset”)
    @com.vaadin.annotations.JavaScript({“https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js”, “script.js”

})
public class MyUI extends UI {

@Override
protected void init(VaadinRequest vaadinRequest) {
    final VerticalLayout layout = new VerticalLayout();
    layout.addStyleName("myLayout");//add class to main div
    
    
    Label label = new Label("Hello Vaadin user. Use this application to upload files.");
    final Button button = new Button("Open popup");
    JavaScript.getCurrent().execute(""                
            +"jsInit();"
        +"");
}

}
[/code]This class calls a javascript file that creates a popup. I need to move this functionality into a new class, so I created a new class, FileUp.java which sits in its own file. I removed what I needed from the MyUI.java and moved it to the new class, but of course it’s not working, and since I’ve never done this before I’m wondering if anybody could give a quick glance over to the new class, here it is:

package my.vaadin.apptest;

import com.vaadin.ui.JavaScript;
import com.vaadin.ui.AbstractJavaScriptComponent;

import elemental.json.JsonObject;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.ui.JavaScript;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;

@com.vaadin.annotations.JavaScript({"https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js", "script.js"
    
})

public class FileUploader extends AbstractJavaScriptComponent {    
        javascript.getcurrent().execute(""                
                +"jsinit();"
            +"");    
    
}

I get java errors:
-Syntax error on token “.”, { expected
-Syntax error on token “getcurrent”, identifier expected after this token
-Syntax error, insert “}” to complete ClassBody

Any advice as I’m not really sure what I’m doing wrong?
thanks

A few points:
-this new class doesn’t have a @Override protected void init(VaadinRequest vaadinRequest) {} but does it in fact need one? I have no idea.
-DO I have to somehow update my pom file?

OK quick update on this.
The compiler complained because of a schoolboy error from my part…I forgot to add a method in the new class! Eh! How silly.
In any case, the new class, FileUploader.java, now looks like this

[code]
package my.vaadin.apptest;
import com.vaadin.ui.AbstractJavaScriptComponent;
import com.vaadin.ui.JavaScript;
@com.vaadin.annotations.JavaScript({“https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js”, “script.js”
})

public class FileUploader extends AbstractJavaScriptComponent {
public void callScript(){
JavaScript.getCurrent().execute(“”
+“jsInit();”
+“”);
}
}
[/code]And here is again the other class, MyUI.java for completeness, now updated:

[code]
package my.vaadin.apptest;
import com.vaadin.ui.AbstractJavaScriptComponent;
import elemental.json.JsonObject;
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.Button.ClickEvent;
import com.vaadin.ui.JavaScript;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.client.ui.*;

@Theme(“mytheme”)
@Widgetset(“my.vaadin.apptest.MyAppWidgetset”)

public class MyUI extends UI {
@Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();
layout.addStyleName(“myLayout”);//add class to main div
Label label = new Label(“Hello Vaadin user. Use this application to upload files.”);
final Button button = new Button(“Open popup”);
button.addStyleName(“popupTriggerBtn”);//add class to button
layout.addComponents( label, 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 {
}

}
[/code]So, now, I take I have to create an object of type FileUpdate() inside MyUI.java so that I can call the javascript jsInit() function that’s currently inside FileUploader.java?
DO I have to import anything before I do that?

Hi,

The class FileUploader is in the same package as your UI class, so you shouldn’t need to import it. IDEs can usually take care of that for you anyway - if you use Eclipse, the Organize Imports command (ctrl+shift+O) will automatically add any missing imports and sort them nicely. You can even add it to your Save Actions in the workspace Preferences to make it happen every time you save your current file (Window → Preferences, then go to Java → Editor → Save Actions).

Best regards,
Olli

thanks I sorted this out eventually, in the MyUI class I created a new FileUploader object and called the FileUploader method from within the click handler:

 final FileUploader fileUploader = new FileUploader();//has to be final apparently     
        
       final Button button = new Button("Open popup");
        button.addClickListener(new Button.ClickListener(){
            @Override
            public void buttonClick(ClickEvent event){
                fileUploader.callScript();
            }
        });