1

How can I generate a Javascript popup from within Java?

I am using the wicket/jquery library.

I tried the below but it failed with an alert method not found error

import javax.script.*;

public class ExecuteScript {
  public static void main(String[] args) throws Exception {
    // create a script engine manager
    ScriptEngineManager factory = new ScriptEngineManager();
    // create a JavaScript engine
    ScriptEngine engine = factory.getEngineByName("JavaScript");
    // evaluate JavaScript code from String
    engine.eval("alert(\"Test\")");
  }
}
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Toosmooth
  • 21
  • 1
  • 4
  • 1
    Javascript is client side and Java is server side...what exactly are you trying to do? – CubeJockey Apr 23 '15 at 14:14
  • Hi, I am really just trying to have a pop up show in my javacode on my UI.. my UI is built with html/wicket/JS.. Where i need to do the logic is within an onsubmit so I do not have access to AjaxRequestTarget target.. Is there a way that in the onSubmit I can have a dialog that gets created and shown immediately without having a click event occur? – Toosmooth Apr 23 '15 at 14:16
  • 1
    @Toosmooth Please take a look at http://stackoverflow.com/questions/11258888/referenceerror-alert-is-not-defined – vinay Apr 23 '15 at 14:19
  • 1
    Do you want a Wicket modal dialog? There's an example here http://www.wicket-library.com/wicket-examples/ajax/modal-window;jsessionid=3223E4C502A5AC35A779F0A7E24C7482?0 – Andrew Fielden Apr 23 '15 at 14:24
  • @AndrewFielden This looks close to what I need which portion of the code do i need where as i can call that class i create from anywhere in my java code and it displays a pop up window – Toosmooth Apr 23 '15 at 14:40

1 Answers1

0

Popup dialogs are normally triggered via clicking on something, either a button, or you can add new AjaxEventBehavior("click") to a Wicket component. The following code fragment gives you an idea of how you could use it with an AjaxButton.

public class MyPanel extends Panel {

  private ModalWindow modalWindow;

  public MyPanel(String id) {

    super(id);

    modalWindow = new ModalWindow("debugDialog");

    add(modalWindow);

    add(new AjaxButton("myButton") {
      protected void onSubmit(AjaxRequestTarget target) {

        Component dbgPanel = // Create a panel to contain the window content
        modalWindow.setContent(dbgPanel);
        modalWindow.show(target);
      }
    }
  }
}
Andrew Fielden
  • 3,751
  • 3
  • 31
  • 47