【问题标题】:Java - How to set max-width for <table> in JTextPane?Java - 如何在 JTextPane 中为 <table> 设置最大宽度?
【发布时间】:2014-10-09 01:13:41
【问题描述】:

我有一个JTextPane,带有 html 内容:

    Pane = new JTextPane();
    Pane.setEditable(false);
    Pane.setContentType("text/html");
    Pane.setEditorKit(kit);
    Pane.setText("<html><body><div id='content'></div></body></html>");
    doc = (HTMLDocument) Pane.getStyledDocument();
    content = doc.getElement("content");

我在运行时添加了我的 html 元素:

doc.insertBeforeEnd(content, "<table class = 'text'><tr><td>"+text+"</td></tr></table>"); // text is input from user

这是我使用的 CSS:

    styleSheet = doc.getStyleSheet();
    styleSheet.addRule("body{background-color:#FFFFFF; margin 0%;}");
    styleSheet.addRule("#content {margin: 0% 10%; width : 80%; }"); // I set them to bring element that will add at the center of `jtextpane` but it did't work!
    styleSheet.addRule("table{ max-width: 60%;}"); // I set this to prevent table with long width, but it didn't work! 
    styleSheet.addRule(".text{background-color:#E2EAF3;margin : 2px;padding : 1px 2px; border-width: 1px;border-style: solid;border-color: #D5D3CD;text-align: right;}");

我还使用 HTMLEditorKit 定义的 here 来包装我的文本。

如何设置 CSS 属性以达到我的目标。

  • 我想为宽度设置最大尺寸,所以如果文本尺寸较长,它会自动换行,如果文本尺寸较短,则背景固定为其内容。

注意:我用于创建 HTMLEditorKit 的代码可以很好地包装文本,问题是设置最大宽度。 (现在它在达到JTextPane 宽度时会换行)

【问题讨论】:

  • 必须等待 StanislavL 或 Andrew Thompson 的评论/回答
  • @mKorbel 我知道如何限制表格的宽度,但问题是从样式中获取最大宽度值

标签: java html css swing jtextpane


【解决方案1】:

不能为表格、行或单元格 (Details on W3 here) 定义 max-width 属性。你应该改用width

【讨论】:

  • 来自您提供的链接 - 属性 'max-width' 适用于:所有元素,但未替换的内联元素、表格行和行组
  • 我已经解释了它应该在桌面上工作的 W3 文档。它确实在 EI11、Chrome V41 和 Firefox V36 中运行良好。
猜你喜欢
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-10
  • 2014-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多