在Swing应用程序中显示并与HTML表单交互 [英] Display and interact with an HTML form in a Swing application

查看:583
本文介绍了在Swing应用程序中显示并与HTML表单交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序会生成一些应该显示在应用程序中的 HTML页面 这些 HTML页面包含一些用于输入一些值的表单



到目前为止,我已经使用了 JTextPane strong>它完美地呈现了 HTML ,但我不知道如何与表单交互以检索用户输入的值。



_



是否可以使用JTextPane / JEditorPane?

如果不是,现在是否有其他方式与HTML表单进行交互?
$ b

_



编辑:以下是 tulskiy 指令的结果:

 <$ c $包装测试; 

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(
新的Runnable()
{
公共无效的run()
{
javax.swing.JFrame中的JF =新javax.swing.JFrame中的() ;
jf.setSize(300,300);
jf.setVisible(真);
jf.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

的JTextPane textPane =新的JTextPane();
textPane.setContentType( text / html的);
textPane.setEditable(假);
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(新HyperlinkListener()
{
@覆盖
公共无效hyperlinkUpdate(HyperlinkEvent E)
{
如果(E的instanceof FormSubmitEvent)
{
System.out.println(((FormSubmitEvent)e).getData());
}
}
});
}
}
);

System.in.read();




$ b $ p
$ b根据用户输入,输出将如下所示:

  input1 =一些+ text& input2 = More + text& cb1 = on& rb1 = on 

请注意, action 属性是必需的,否则会引发异常。



_



感谢您提供任何提示。

解决方案

我相信如果你的表单上有一个提交按钮,它应该工作并发送数据到服务器。我不确定你是否可以在代码中与它进行交互。这些元素被渲染为swing组件,因此理论上你可以从JTextPane中获取所有组件,并找到你的按钮和输入字段。

这是在JEditorPane中,你需要将submition属性的auto设置为false

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

然后您将能够使用编辑器窗格注册一个超链接监听器,并且您将收到一个 FormSubmitEvent 。它有url和方法,所以你可以解码一些数据。


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.

解决方案

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.

这篇关于在Swing应用程序中显示并与HTML表单交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆