gpt4 book ai didi

javascript - Swing html中的Java回调

转载 作者:行者123 更新时间:2023-12-03 06:38:34 24 4
gpt4 key购买 nike

我们正在使用 HTML 在 swing 应用程序中绘制表格

String html = generateHtml();
this.textPane = new JTextPane();
HTMLEditorKit kit = new HTMLEditorKit();
textPane.setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("th, td {width: 50px; text-align: center;}");
Document doc = kit.createDefaultDocument();
textPane.setDocument(doc);
textPane.setContentType("text/html");
textPane.setText(html);
textPane.setEditable(false);
textPane.setBackground(null);
textPane.setBorder(null);
textPane.putClientProperty(JTextPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
this.add(textPane);

我希望能够在其中一个表格单元格内单击,以调用 java 类回调,也许可以通过 javascript?或者任何其他选择?
例如,单击一个表格单元格,该单元格将调用 java 方法来做一些工作,并打开一些对话框窗口。
也许这可以通过javascript来回调java来完成?或者直接从html转java?

我知道有第三方库,例如 JxBrowser ,像这样example但是,我们首先寻找是否有一种方法可以通过使用内置组件来做到这一点,而无需将第 3 方库添加到分布式应用程序中。

最佳答案

I would like to be able to click inside one of the table cells, to invoke a java class callback, maybe through javascript? or any other option? For example, to click a table cell that will call a java method to do some work, and open some dialog window.

假设您正在显示相对简单的内容,您可以将表格单元格的内容包装在链接中,并使用超链接监听器来处理链接上的单击事件。一个简单的例子:

JTextPane editor = new JTextPane();
editor.setContentType("text/html");
editor.setText("<html><body><table><tr><td><a href=\"a1\" >Cell1</a></td><td><a href=\"a2\">Cell2</a></td></tr></table></body></html>");
editor.addHyperlinkListener(new HyperlinkListener(){

@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if ( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED ){
System.out.println(e.getDescription());
}

}

});
editor.setEditable(false);

如果您不希望使用默认样式(颜色、下划线等),则需要为链接设置其他样式。

关于javascript - Swing html中的Java回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38079432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com