【问题标题】:Listen to changes of field by Swt Eclipse browser通过 Swt Eclipse 浏览器监听字段的变化
【发布时间】:2017-02-23 00:17:29
【问题描述】:

在 Java 项目中,我使用 SWT Eclipse 作为浏览器。

我运行这个浏览器并加载页面正常。

加载的页面是这样的:

<!DOCTYPE html>
<html>
<body>

Start: <input type="text" id="startTimeField" onchange="myFunction()" placeholder="Type text and press Enter"> ms

<script>
    function myFunction() {
       document.getElementById('startTimeField').value = new Date().getMilliseconds();
    }
</script>

</body>
</html>

我想在 Java 代码中监听页面中的元素。

示例我想要实现的目标: 在 Java 中按 JButton,它应该从页面中找到 ID 为 startTimeField 的按钮并向它们添加侦听器。侦听器应侦听此字段的更改值。这个怎么做 ?

【问题讨论】:

  • 看起来您打算混合使用 SWT 和 Swing/AWT。如果可以避免,请不要走那条路。但是,您可能应该查看 this 以了解您正在寻找的 JavaJavascript 交互。
  • 我想在 Java 代码中添加监听器来监听 SWT 页面中文本输入的变化。在question 中执行与使用 JxBrowser 相同的操作
  • 不确定是否可以在不修改 HTML 的情况下实现。
  • 那你建议怎么做?
  • 在 html 中添加监听器并从监听器中调用 Java 浏览器函数。

标签: javascript java html eclipse swt


【解决方案1】:

这是一个非常简单的示例,其中包含两个 HTML 按钮。单击时,他们将调用 SWT BrowserFunction 并传入按钮的 id:

public static void main(String[] args)
{

    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    Browser browser = new Browser(shell, SWT.NONE);
    browser.setText("<button id='button1' onclick='buttonClicked(this.id)'>Click me</button><button id='button2' onclick='buttonClicked(this.id)'>Click me</button>");

    new BrowserFunction(browser, "buttonClicked")
    {
        @Override
        public Object function(Object[] objects)
        {
            System.out.println("BUTTON CLICK; ID: " + objects[0]);
            return null;
        }
    };

    shell.pack();
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 2015-10-26
    相关资源
    最近更新 更多