Display and interact with an HTML form in a Swing application
Asked Answered
P

1

11

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.

Propylaeum answered 26/5, 2011 at 21:38 Comment(1)
can you possibly provide some code, something to start playing with?Provitamin
L
7

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.

Lantha answered 27/5, 2011 at 7:32 Comment(3)
in theory this should be something like this but after googling for a similar use-case I've found no code that does it or something similar. Playing with the component with Eclipse debug watch capabilities did not help too. Maybe there is another way to get the data, e.g. by catching the POST data when the form is submitted.Propylaeum
glade to see that catching the submit was not an absurd idea. Following your great hints, ie disabling the auto form submission and registering for "FormSubmitEvent" hyperlink event, did the job. I'll edit my original post with some code. Thanks a lot !Propylaeum
@Serious: you're welcome. it's weird that it was kinda hard to find any documentation on this. One more thing - check that the event you receive is actually a FormSubmitEvent, because if you add any other hyperlinks to the form later, you can have a ClassCastException.Lantha

© 2022 - 2024 — McMap. All rights reserved.