在jTextPane(或替代)中听HTML复选框?

Ogn*_*jen 2 html java swing jtextpane

我使用不可编辑的JTextPane来显示HTML格式的一些数据.我已将contentType设置为"text/html"并且它可以正常工作.现在我想在JTextPane中添加HTML复选框,并听取他们的更改,并能够检索是否选中了特定的复选框.这可能吗?

JTextPane的文本采用以下格式:

<html><form>
<input type="checkbox" name="checkbox1" value="value" /> checkbox1<br />
</form></html>
Run Code Online (Sandbox Code Playgroud)

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

cam*_*ckr 7

通常,您将使用JEditorPane来显示HTML.

根据您的要求,有两种方法可以解决这个问题:

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

  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); }
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)