简体   繁体   English

在 Swing 应用程序中显示 HTML 表单并与之交互

[英]Display and interact with an HTML form in a Swing application

an application generates some HTML pages that should be displayed in the application itself.应用程序会生成一些HTML 页面,这些页面应该显示在应用程序本身中。

These HTML pages contain some forms that would be used by the user to enter some values.这些HTML 页面包含一些forms供用户输入一些值。

So far I've used a JTextPane which renders the HTML perfectly, but I do not know how to interact with the form to retrieve the values entered by the user.到目前为止,我使用了一个JTextPane ,它完美地呈现了HTML ,但我不知道如何与表单交互以检索用户输入的值。

_ _

Is it possible to do so with a JTextPane / JEditorPane?是否可以使用 JTextPane / JEditorPane 这样做?

If no, do you now any other way to interact with an HTML form?如果没有,您现在是否有任何其他方式与 HTML 表单进行交互?

_ _

EDIT : following tulskiy instructions here is the result:编辑:按照tulskiy说明,结果如下:

package tests;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;

import org.junit.Test;


public class JTextPaneTests
{
    @Test
    public void testForms() throws Exception
    {
        javax.swing.SwingUtilities.invokeLater(
            new Runnable()
            {
                public void run()
                {
                    javax.swing.JFrame jf = new javax.swing.JFrame();
                    jf.setSize(300,300);
                    jf.setVisible(true);
                    jf.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

                    JTextPane textPane = new JTextPane();
                    textPane.setContentType("text/html");
                    textPane.setEditable(false);
                    textPane.setText(
                            "<html>" +
                                "<body>" +
                                    "<form action=\"#\">" +
                                        "<input name=\"input1\" type=\"text\" />" +
                                        "<input name=\"input2\" type=\"text\" /><br/>" +
                                        "<input name=\"cb1\" type=\"checkbox\" /><br/>" +
                                        "<input name=\"rb1\" type=\"radio\" /><br/>" +
                                        "<input type=\"submit\" value=\"go\" />" +
                                    "</form>" +
                                "</body>" +
                            "</html>");

                    jf.getContentPane().setLayout(new BoxLayout(jf.getContentPane(), BoxLayout.Y_AXIS));

                    jf.getContentPane().add(textPane);

                    HTMLEditorKit kit = (HTMLEditorKit)textPane.getEditorKit();
                    kit.setAutoFormSubmission(false);
                    textPane.addHyperlinkListener(new HyperlinkListener()
                    {                           
                        @Override
                        public void hyperlinkUpdate(HyperlinkEvent e)
                        {
                            if (e instanceof FormSubmitEvent)
                            {
                                System.out.println(((FormSubmitEvent)e).getData());
                            }
                        }
                    });
                }
            }
        );

        System.in.read();
    }
}

Depending on the user inputs the output will be like:根据用户输入,output 将如下所示:

input1=Some+text&input2=More+text&cb1=on&rb1=on

Note that the " action " attribute is mandatory, otherwise an exception is thrown.注意“ action ”属性是强制的,否则抛出异常。

_ _

Thanks in advance for any hint.提前感谢您的任何提示。

I believe if you have a submit button on your form, it should work and send data to server.我相信如果您的表单上有一个提交按钮,它应该可以工作并将数据发送到服务器。 I'm not sure if you can interact with it in the code.我不确定您是否可以在代码中与之交互。 Those elements are rendered as swing component, so in theory you get all components from the JTextPane and find your button and input fields.这些元素被渲染为 swing 组件,因此理论上您可以从 JTextPane 获取所有组件并找到您的按钮和输入字段。

EDIT To do this is in JEditorPane, you need to set auto for submition property to false编辑要在 JEditorPane 中执行此操作,您需要将提交属性的 auto 设置为 false

((HTMLEditorKit)textPane.getEditorKit()).setAutoFormSubmission(false);

then you will be able to register a hyperlink listener with the editor pane and you will be receiving a FormSubmitEvent .那么您将能够在编辑器窗格中注册一个超链接侦听器,并且您将收到一个FormSubmitEvent It has url and method, so you can decode some data from it.它具有 url 和方法,因此您可以从中解码一些数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM