监听 jTextPane 中的 HTML 复选框(或替代方案)?

发布于 2024-12-13 11:17:13 字数 407 浏览 0 评论 0原文

我正在使用不可编辑的 JTextPane 以 HTML 格式显示一些数据。我已将 contentType 设置为“text/html”并且它有效。现在我想在 JTextPane 中添加 HTML 复选框,并监听它们的更改,并且能够检索是否选择了特定的复选框。这可能吗?

JTextPane 的文本采用以下格式:

<html><form>
<input type="checkbox" name="checkbox1" value="value" /> checkbox1<br />
</form></html>

我应该将 JTextPane 用于此目的吗,还是有更好的控制?常规复选框不是一个选项,因为我需要 HTML 格式来轻松设置样式。

I am using non-editable JTextPane to show some data in HTML format. I have set contentType to "text/html" and it works. Now I wanted to add HTML checkboxes in JTextPane, and listen to their change, and be able to retrieve if a specific checkBox is selected. Is this possible?

The text of JTextPane is in this format:

<html><form>
<input type="checkbox" name="checkbox1" value="value" /> checkbox1<br />
</form></html>

Should I be using JTextPane for this purpose at all, or is there a better control? Regular check boxes are not an option, because I need a HTML format for styling it easily.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ヅ她的身影、若隐若现 2024-12-20 11:17:13

通常您会使用 JEditorPane 来显示 HTML。

根据您的要求,有两种方法可以实现此目的:

  1. Swing 组件实际上添加到编辑器窗格中。因此,一旦解析了文档并且编辑器窗格已被重新验证(),您应该能够获得添加到编辑器窗格的所有组件的列表。您可以检查类名来查找所需的组件。

  2. HTMLDocument 包含有关添加的每个组件的属性,包括组件模型。因此,您可以搜索文档以获取每个复选框的模型。

以下是一些可帮助您入门的通用代码:

import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class GetComponent extends JFrame
{
    public GetComponent()
        throws Exception
    {
        FileReader reader = new FileReader("form.html");

        JEditorPane editor = new JEditorPane();
        editor.setContentType( "text/html" );
        editor.setEditable( false );
        editor.read(reader, null);

        JScrollPane scrollPane = new JScrollPane( editor );
        scrollPane.setPreferredSize( new Dimension(400, 300) );
        add( scrollPane );

        setDefaultCloseOperation( EXIT_ON_CLOSE );
        pack();
        setLocationRelativeTo( null );
        setVisible(true);

        //  display the attributes of the document

        HTMLDocument doc = (HTMLDocument)editor.getDocument();
        ElementIterator it = new ElementIterator(doc);
        Element element;

        while ( (element = it.next()) != null )
        {
            System.out.println();

            AttributeSet as = element.getAttributes();
            Enumeration enumm = as.getAttributeNames();

            while( enumm.hasMoreElements() )
            {
                Object name = enumm.nextElement();
                Object value = as.getAttribute( name );
                System.out.println( "\t" + name + " : " + value );

                if (value instanceof DefaultComboBoxModel)
                {
                    DefaultComboBoxModel model = (DefaultComboBoxModel)value;

                    for (int j = 0; j < model.getSize(); j++)
                    {
                        Object o = model.getElementAt(j);
                        Object selected = model.getSelectedItem();
                        System.out.print("\t\t");

                        if ( o.equals( selected ) )
                            System.out.println( o + " : selected" );
                        else
                            System.out.println( o );
                    }
                }
            }
        }

        //  display the components added to the editor pane

        for (Component c: editor.getComponents())
        {
            Container parent = (Container)c;
            System.out.println(parent.getComponent(0).getClass());
        }
    }

    public static void main(String[] args)
        throws Exception
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    GetComponent frame = new GetComponent();
                }
                catch(Exception e) { System.out.println(e); }
            }
        });
    }
}

Generally you would use a JEditorPane to display HTML.

Depending on your requirement there are two ways to go about this:

  1. Swing components are actually added to the editor pane. So once the docuemnt has been parsed and the editor pane has been revalidated() you should be able to just get a list of all the components added to the editor pane. You can check the class name to find the components you want.

  2. The HTMLDocument contains attributes about each component added including the components model. So you can search the document to get the model for every checkbox.

Here is some general code to get your started:

import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class GetComponent extends JFrame
{
    public GetComponent()
        throws Exception
    {
        FileReader reader = new FileReader("form.html");

        JEditorPane editor = new JEditorPane();
        editor.setContentType( "text/html" );
        editor.setEditable( false );
        editor.read(reader, null);

        JScrollPane scrollPane = new JScrollPane( editor );
        scrollPane.setPreferredSize( new Dimension(400, 300) );
        add( scrollPane );

        setDefaultCloseOperation( EXIT_ON_CLOSE );
        pack();
        setLocationRelativeTo( null );
        setVisible(true);

        //  display the attributes of the document

        HTMLDocument doc = (HTMLDocument)editor.getDocument();
        ElementIterator it = new ElementIterator(doc);
        Element element;

        while ( (element = it.next()) != null )
        {
            System.out.println();

            AttributeSet as = element.getAttributes();
            Enumeration enumm = as.getAttributeNames();

            while( enumm.hasMoreElements() )
            {
                Object name = enumm.nextElement();
                Object value = as.getAttribute( name );
                System.out.println( "\t" + name + " : " + value );

                if (value instanceof DefaultComboBoxModel)
                {
                    DefaultComboBoxModel model = (DefaultComboBoxModel)value;

                    for (int j = 0; j < model.getSize(); j++)
                    {
                        Object o = model.getElementAt(j);
                        Object selected = model.getSelectedItem();
                        System.out.print("\t\t");

                        if ( o.equals( selected ) )
                            System.out.println( o + " : selected" );
                        else
                            System.out.println( o );
                    }
                }
            }
        }

        //  display the components added to the editor pane

        for (Component c: editor.getComponents())
        {
            Container parent = (Container)c;
            System.out.println(parent.getComponent(0).getClass());
        }
    }

    public static void main(String[] args)
        throws Exception
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    GetComponent frame = new GetComponent();
                }
                catch(Exception e) { System.out.println(e); }
            }
        });
    }
}
迷路的信 2024-12-20 11:17:13

我不认为你可以在 JTextPane 中处理 javascript 事件,所以我不认为切换复选框是一个选项。

I don't think you could handle javascript events in JTextPane, so I don't think toggling the checkbox is an option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文