[英]swing java app and iframe - two way communication
我有一个 java swing 应用程序,想在 iframe 中呈现 html 页面。 根据我在 iframe 中执行的操作,是否可以将参数或数据传回我的 java swing 应用程序?
谢谢
您可以查看JxBrowser库,该库允许将 Google Chromium 引擎嵌入到 Java Swing 应用程序中。
它提供了 Java-to-JavaScript-to-Java 双向通信 API: http : //www.teamdev.com/downloads/jxbrowser/docs/JxBrowser-PGuide.html#javascript-java-bridge
以下代码演示了如何嵌入浏览器组件、加载 URL、在加载的网页上调用 JavaScript 代码以及在 JavaScript 端注册 Java 函数,该函数将在每次 JavaScript 调用时调用:
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserFactory;
import com.teamdev.jxbrowser.chromium.BrowserFunction;
import com.teamdev.jxbrowser.chromium.JSValue;
import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent;
import com.teamdev.jxbrowser.chromium.events.LoadAdapter;
/**
* The sample demonstrates how to register a new JavaScript function and
* map it to a Java method that will be invoked every time when the JavaScript
* function is invoked.
*/
public class JavaScriptJavaSample {
public static void main(String[] args) {
Browser browser = BrowserFactory.create();
// Register "MyFunction" JavaScript function and associate Java callback with it
browser.registerFunction("MyFunction", new BrowserFunction() {
public JSValue invoke(JSValue... args) {
for (JSValue arg : args) {
System.out.println("arg = " + arg);
}
return JSValue.create("Hello!");
}
});
// Create JFrame and embed Browser component to display web pages
JFrame frame = new JFrame();
frame.add(browser.getView().getComponent(), BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Register Load listener to get notification when web page is loaded completely
browser.addLoadListener(new LoadAdapter() {
@Override
public void onFinishLoadingFrame(FinishLoadingEvent event) {
if (event.isMainFrame()) {
Browser browser = event.getBrowser();
// Invoke our registered JavaScript function
JSValue returnValue = browser.executeJavaScriptAndReturnValue(
"MyFunction('Hello JxBrowser!', 1, 2, 3, true);");
System.out.println("return value = " + returnValue);
}
}
});
browser.loadURL("about:blank");
}
}
通常,您会使用 JEditorPane 来显示 HTML。正如 Software Monkey 所说,Swing 支持 HTML 3.2 (Wilbur)。 您可以在以下网址找到此过时 (1996) 版 HTML 的官方文档: http : //www.w3.org/TR/REC-html32.html
关于该主题的 Java 7 文档: http : //docs.oracle.com/javase/7/docs/api/javax/swing/text/html/package-summary.html
尽管值得注意的是,它没有明确提及此信息对其他 Swing 组件有效。
根据您的要求,有两种方法可以解决此问题:
Swing 组件实际上已添加到编辑器窗格中。 因此,一旦解析了文档并且重新验证了编辑器窗格,您就应该能够获得添加到编辑器窗格的所有组件的列表。 您可以检查类名以查找所需的组件。
HTMLDocument 包含有关添加的每个组件的属性,包括组件模型。 因此,您可以搜索文档以获取每个复选框的模型。
以下是一些让您入门的通用代码:
import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class GetComponent extends JFrame
{
public GetComponent()
throws Exception
{
FileReader reader = new FileReader("form.html");
JEditorPane editor = new JEditorPane();
editor.setContentType( "text/html" );
editor.setEditable( false );
editor.read(reader, null);
JScrollPane scrollPane = new JScrollPane( editor );
scrollPane.setPreferredSize( new Dimension(400, 300) );
add( scrollPane );
setDefaultCloseOperation( EXIT_ON_CLOSE );
pack();
setLocationRelativeTo( null );
setVisible(true);
// display the attributes of the document
HTMLDocument doc = (HTMLDocument)editor.getDocument();
ElementIterator it = new ElementIterator(doc);
Element element;
while ( (element = it.next()) != null )
{
System.out.println();
AttributeSet as = element.getAttributes();
Enumeration enumm = as.getAttributeNames();
while( enumm.hasMoreElements() )
{
Object name = enumm.nextElement();
Object value = as.getAttribute( name );
System.out.println( "\t" + name + " : " + value );
if (value instanceof DefaultComboBoxModel)
{
DefaultComboBoxModel model = (DefaultComboBoxModel)value;
for (int j = 0; j < model.getSize(); j++)
{
Object o = model.getElementAt(j);
Object selected = model.getSelectedItem();
System.out.print("\t\t");
if ( o.equals( selected ) )
System.out.println( o + " : selected" );
else
System.out.println( o );
}
}
}
}
// display the components added to the editor pane
for (Component c: editor.getComponents())
{
Container parent = (Container)c;
System.out.println(parent.getComponent(0).getClass());
}
}
public static void main(String[] args)
throws Exception
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
GetComponent frame = new GetComponent();
}
catch(Exception e) { System.out.println(e); }
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.