English 中文(简体)
来自Javac的 Call
原标题:Call Java from JavaScript gwt
  • 时间:2011-11-23 10:39:05
  •  标签:
  • java
  • gwt

Hi I have the following strange issue:

I have gwt application. it contains 3 image widgets and a button. on Button click I am calling function X() and then inside X I am calling Test(testscan instance) by passing instance to the Javascript function.

现在看着试验功能。 我试图把 Java称为“最新消息”的职能从那里传来。 The syntax written at GWT documentation.

When I run the application the instance of "testscan" class is Null what can be the reason?

public class testscan implements EntryPoint {
private Image image_0 = new Image("home.gif");
public void onModuleLoad() {
    RootPanel rootPanel = RootPanel.get("main_panel");
    rootPanel.getElement().getStyle().setPosition(Position.RELATIVE);

    VerticalPanel verticalPanel = new VerticalPanel();
    rootPanel.add(verticalPanel, 5, 5);
    verticalPanel.setSize("100%", "100%");

    HorizontalPanel horizontalPanel = new HorizontalPanel();
    verticalPanel.add(horizontalPanel);
    horizontalPanel.setSize("100%", "100%");

    Image image_1 = new Image("home.gif");
    horizontalPanel.add(image_1);
    image_1.setSize("180px", "180px");

    horizontalPanel.add(image_0);
    image_0.setSize("180px", "180px");

    Image image_2 = new Image("home.gif");
    horizontalPanel.add(image_2);
    image_2.setSize("180px", "180px");

    Button btnScan = new Button("Scan");
    verticalPanel.add(btnScan);

    btnScan.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {

            x();
        }
    });

}
public void x()
{
    Test(this);

}

public native void Test(testscan instance) /*-{
    [email protected]::updateImageContent()();
}-*/;

public void updateImageContent()
{
    Window.alert("ok");
    //String s = scanFileContent();
    //Window.alert(s);
    if(image_0==null)
        Window.alert("bad");
    else
        Window.alert("good");

}
问题回答

I think your issue is with the way your ClickHandler is implemented as an anonymous inner class. At the point when Test method is executed, this has no meaning anymore. You need to rewrite your ClickHandler to hold an instance to your class:

public class testscan implements EntryPoint {
public static class MyClickHandler implements ClickHandler {
   testscan instance;
   public MyClickHandler(testscan instance) {
      this.instance = instance;
   }

   public void onClick(ClickEvent event) {
            this.instance.x();
   }
}
private Image image_0 = new Image("home.gif");
public void onModuleLoad() {
    RootPanel rootPanel = RootPanel.get("main_panel");
    rootPanel.getElement().getStyle().setPosition(Position.RELATIVE);

    VerticalPanel verticalPanel = new VerticalPanel();
    rootPanel.add(verticalPanel, 5, 5);
    verticalPanel.setSize("100%", "100%");

    HorizontalPanel horizontalPanel = new HorizontalPanel();
    verticalPanel.add(horizontalPanel);
    horizontalPanel.setSize("100%", "100%");

    Image image_1 = new Image("home.gif");
    horizontalPanel.add(image_1);
    image_1.setSize("180px", "180px");

    horizontalPanel.add(image_0);
    image_0.setSize("180px", "180px");

    Image image_2 = new Image("home.gif");
    horizontalPanel.add(image_2);
    image_2.setSize("180px", "180px");

    Button btnScan = new Button("Scan");
    verticalPanel.add(btnScan);

    btnScan.addClickHandler(new MyClickHandler(this));

}
public void x()
{
    Test(this);

}

public native void Test(testscan instance) /*-{
    [email protected]::updateImageContent()();
}-*/;

public void updateImageContent()
{
    Window.alert("ok");
    //String s = scanFileContent();
    //Window.alert(s);
    if(image_0==null)
        Window.alert("bad");
    else
        Window.alert("good");

}
}

我指的是,你以这种整体方式来处理这个问题,不过,我理解,你只是学习,这只是你正在经历的考验。 通常,在您的切入点类别中,你不会有这种编号。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签