Display and interact with an HTML form in a Swing

2019-02-09 05:01发布

问题:

an application generates some HTML pages that should be displayed in the application itself.

These HTML pages contain some forms that would be used by the user to enter some values.

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.

_

Is it possible to do so with a JTextPane / JEditorPane ?

If no, do you now any other way to interact with an HTML form ?

_

EDIT : following tulskiy instructions here is the result :

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 :

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

Note that the "action" attribute is mandatory, otherwise an exception is thrown.

_

Thanks in advance for any hint.

回答1:

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.

EDIT To do this is in JEditorPane, you need to set auto for submition property to 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. It has url and method, so you can decode some data from it.