Java - How to completly remove the line spacing (vertical gap) and character spacing (horizontal gap) in a JTextPane?

StackOverflow https://stackoverflow.com/questions/22416502

Frage

I am failing to completly remove line-spacing and character spacing from a JTextPane. Is this even possible?

Google showed me following code-snippet, but it appears to have no effect at all on my JTextPane, and as far as I understand it it would only get rid of the vertical gaps (spaces between lines).

SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setLineSpacing(sas , 0.0f);
textPane.setParagraphAttributes(sas , false);

I currently have set the JTextPane's font to "Consolas", font-size 9. I am trying to display certain ASCII art correctly (just like DAMN NFO VIEWER does it).

This is what it looks like in my GUI's JTextPane and how I would want it to look like:

ascii editor spacing visualization

This is how DAMN NFO VIEWER displays the same ASCII art (the correct way).

spacing done correctly

I've tried using the same font and size that DAMN NFO VIEWER is using but that doesn't get rid of the gaps, so I figured I have to do some other changes. Any suggestions appreciated.

EDIT

Here is how I attempt to apply the SimpleAttributeSet(), maybe I am not doing it right:

nfoFileTextPane = new javax.swing.JTextPane();
StyledDocument doc = nfoFileTextPane.getStyledDocument();
Element e = doc.getParagraphElement(0);
MutableAttributeSet mas = new SimpleAttributeSet(); 
StyleConstants.setLineSpacing(mas, -0.2f);
StyleConstants.setSpaceAbove(mas, -0.2f);
StyleConstants.setSpaceBelow(mas, -0.2f);
//0-1000 test
doc.setParagraphAttributes(0, 1000, mas, true);
War es hilfreich?

Lösung

The text pane may well not give you the level of control you need for this. Even if you adjust the gap between characters to a negative value you will probably find that you need to adjust different characters by different amounts to allow for kerning, etc. If you use an Image instead and do your own rendering of the text you can get really fine control of the position and place it exactly where you need.

This question I asked last year may help you if you decide to do that:

Rendering multi-line multi-page formatted text to a BufferedImage (not in Android)

Andere Tipps

Actually the SimpleAttributeSet was applied correctly using the piece of code above. Just the line-spacing was even bigger and -0.2f was not a sufficient value to close the gap between the lines for the specified font and size. Using -1.0f would squeeze the lines together and make them overlap.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top