Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't remove extra line spacing in JTextPane for html content
    text
    copied!<p>I can't squeeze together the lines in a Java <code>JTextPane</code> if I set the content-type to <code>text/html</code>. I would like them as close together as they are when the content-type is <code>text/plain</code>, the default.</p> <p>The <code>line-height</code>, <code>top-margin</code>, ... CSS properties don't seem to help :(.</p> <p>This is the output of my sample program, that shows that the lines do take more space when a HTML editor handles the rendering:</p> <p><a href="http://lh6.ggpht.com/_Wx4sMDdKKdU/S8cYWIPKhzI/AAAAAAAAAig/4QzFwygmEBs/simpleTextPane.PNG" rel="nofollow noreferrer">alt text http://lh6.ggpht.com/_Wx4sMDdKKdU/S8cYWIPKhzI/AAAAAAAAAig/4QzFwygmEBs/simpleTextPane.PNG</a></p> <p>The code that generates the frame is:</p> <pre><code>import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JTextPane; import javax.swing.text.StyleConstants; import javax.swing.text.html.HTMLEditorKit; import javax.swing.text.html.StyleSheet; public class DemoSimplestGui extends JFrame { private static final long serialVersionUID = 1L; private static final int WINDOW_WIDTH = 800; private static final int WINDOW_HEIGHT = 130; private static final String PLAIN_TEXT = "" + "This is some &lt;b&gt;plain text&lt;/b&gt;\n" + "separated by backslash-n characters\n" + "There's no empty space between lines\n" + "which is exactly what we need."; private static final String DIV_BASED_HTML_TEXT = "" + "&lt;div&gt;This is some &lt;b&gt;html text&lt;/b&gt;&lt;/div&gt;" + "&lt;div&gt;that usses DIV tags.&lt;/div&gt;" + "&lt;div&gt;There's too much blank space&lt;/div&gt;" + "&lt;div&gt;and that sucks for my application&lt;/div&gt;"; private static final String PRE_BASED_HTML_TEXT = "" + "&lt;pre&gt;This is some &lt;b&gt;html text&lt;/b&gt;&lt;/pre&gt;" + "&lt;pre&gt;that usses PRE tags&lt;/pre&gt;" + "&lt;pre&gt;There's too much blank space grr&lt;/pre&gt;" + "&lt;pre&gt;and that sucks for my application&lt;/pre&gt;"; public static void main(String[] args) { final DemoSimplestGui frame = new DemoSimplestGui(); frame.setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT)); frame.setSize(frame.getPreferredSize()); frame.setMinimumSize(new Dimension(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2)); frame.init(); frame.setVisible(true); } public void init() { setLayout(new BorderLayout(10, 10)); add(createPlainTextPane(), BorderLayout.WEST); add(createDivBasedHtmlTextPane(), BorderLayout.CENTER); add(createPreBasedHtmlTextPane(), BorderLayout.EAST); } private JTextPane createPlainTextPane() { final JTextPane textPane = new JTextPane(); textPane.setContentType("text/plain"); StyleConstants.setFontFamily(textPane.getInputAttributes(), "Courier New"); textPane.setText(PLAIN_TEXT); return textPane; } private JTextPane createDivBasedHtmlTextPane() { final JTextPane textPane = new JTextPane(); textPane.setContentType("text/html"); textPane.setEditorKit(configureHtmlEditorKit(textPane)); textPane.setText(DIV_BASED_HTML_TEXT); return textPane; } private JTextPane createPreBasedHtmlTextPane() { final JTextPane textPane = new JTextPane(); textPane.setContentType("text/html"); textPane.setEditorKit(configureHtmlEditorKit(textPane)); textPane.setText(PRE_BASED_HTML_TEXT); return textPane; } private HTMLEditorKit configureHtmlEditorKit(JTextPane textPane) { final HTMLEditorKit kit = (HTMLEditorKit) textPane.getEditorKit(); final StyleSheet css = new StyleSheet(); css.addRule("body { font-family: monospaced; margin-top: 0; margin-down: 0; line-height: 0; }"); css.addRule("div, pre { margin-top: 0; margin-down: 0; line-height: 0; }"); kit.setStyleSheet(css); return kit; } } </code></pre> <p>I'd really appreciate some hint :D</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload