简体   繁体   English

Java Swing JTextPane text/html 不支持禁用属性

[英]Java Swing JTextPane text/html not honoring disabled attribute

I am trying to display a simple HTML page in a JTextPane.我正在尝试在 JTextPane 中显示一个简单的 HTML 页面。 The page has input checkbox/radio elements but they need to be disabled.该页面具有输入复选框/单选元素,但需要禁用它们。 However, when the page is displayed, the checkbox and radio buttons are still enabled.但是,当页面显示时,复选框和单选按钮仍处于启用状态。 I don't want the user to be able to click and change their state.我不希望用户能够点击并改变他们的状态。 Is this a bug or I am doing something wrong.这是一个错误还是我做错了什么。 Please help.请帮忙。 Here is sample code:这是示例代码:

import javax.swing.*;
public class JEPTest {
    public static void main(String[] args) throws Exception{
        String data = "<html>\n" +
"\n" +
"<body>\n" +
"<input type='radio' disabled>\n" +
"\n" +
"</body>\n" +
"</html>";

    final JEditorPane textPane = new JEditorPane();
        textPane.setContentType("text/html");
        textPane.setEnabled(false);
        JScrollPane paneScrollPane = new JScrollPane(textPane);
        paneScrollPane.setVerticalScrollBarPolicy(
                        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        paneScrollPane.setHorizontalScrollBarPolicy(
                        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        //paneScrollPane.setPreferredSize(new Dimension(250, 155));
        paneScrollPane.setMinimumSize(new Dimension(100, 100));        


    JButton jb = new JButton("set");
    jb.addActionListener
        (
        new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
        textPane.setText(data);

        }
    }
        );

    JFrame jf = new JFrame();
    jf.setSize(800, 600);       

    jf.getContentPane().add(textPane, BorderLayout.CENTER);
    jf.getContentPane().add(jb, BorderLayout.SOUTH);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setVisible(true);

    }
}

Swing uses an old HTML rendering engine which only supports HTML 3.2. Swing 使用仅支持 HTML 3.2 的旧 HTML 渲染引擎。 The HTML 3.2 specification reveals that the <input> element did not allow for a disabled attribute at the time of that version. HTML 3.2 规范显示<input>元素在该版本时不允许使用disabled属性。 The defined attributes were:定义的属性是:

<!ATTLIST INPUT
        type %InputType TEXT     -- what kind of widget is needed --
        name  CDATA #IMPLIED     -- required for all but submit and reset --
        value CDATA #IMPLIED     -- required for radio and checkboxes --
        checked (checked) #IMPLIED -- for radio buttons and check boxes --
        size CDATA  #IMPLIED     -- specific to each type of field --
        maxlength NUMBER #IMPLIED
        src   %URL  #IMPLIED     -- for fields with background images --
        align %IAlign #IMPLIED   -- vertical or horizontal alignment --
        >

The best workaround is to avoid using JEditorPane and just create a JCheckBox instance in a JPanel.最好的解决方法是避免使用 JEditorPane 而只是在 JPanel 中创建一个 JCheckBox 实例。

Alternatively, you can embed any visual Java bean, including all JComponent descendants, in a JEditorPane's HTML document, using an <object> element.或者,您可以使用<object>元素在 JEditorPane 的 HTML 文档中嵌入任何可视 Java bean,包括所有 JComponent 后代。 This is described in the javax.swing.text.html.ObjectView documentation.这在javax.swing.text.html.ObjectView文档中有所描述。

You can use this capability to embed a JRadioButton directly.您可以使用此功能直接嵌入 JRadioButton。 However, only String properties can be set this way, so you will have to create your own subclass of JRadioButton and add a String property which wraps the enabled property:但是,只能以这种方式设置 String 属性,因此您必须创建自己的 JRadioButton 子类并添加一个 String 属性来包装enabled属性:

public class JEPTest {
    public static class StringPropRadioButton
    extends JRadioButton {
        private static final long serialVersionUID = 1;

        public String getEnabledAsString() {
            return String.valueOf(isEnabled());
        }

        public void setEnabledAsString(String enabled) {
            setEnabled(Boolean.parseBoolean(enabled));
        }
    }

    public static void main(String[] args) throws Exception{
        String data = "<html>\n" +
        "\n" +
        "<body>\n" +
        "<input type='radio' disabled>\n" +
        "\n" +
        "<p>" +
            "<object classid='JEPTest$StringPropRadioButton' id='option1'>" +
            "    <param name='text' value='Option 1'>" +
            "    <param name='enabledAsString' value='false'>" +
            "</object>" +
        "</body>\n" +
        "</html>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM