【问题标题】:Listening to HTML check boxes in jTextPane (or an alternative)?聆听 jTextPane(或替代方案)中的 HTML 复选框?
【发布时间】:2011-12-18 23:48:56
【问题描述】:

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

JTextPane 的文本格式如下:

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

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

【问题讨论】:

    标签: java html swing jtextpane


    【解决方案1】:

    通常您会使用 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); }
                }
            });
        }
    }
    

    【讨论】:

      【解决方案2】:

      我认为您无法在 JTextPane 中处理 javascript 事件,因此我认为切换复选框不是一种选择。

      【讨论】:

        猜你喜欢
        • 2015-05-08
        • 1970-01-01
        • 1970-01-01
        • 2011-05-16
        • 2012-04-09
        • 2012-01-21
        • 2013-02-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多