Line spacing in iText or JTextArea list item

I need to synchronize the line spacing of my PDF ListItem and JTextArea in my GUI. I can do this by tuning this or that.

And all this works fine if the ListItem (or JTextArea) has a length of more than one line (JTextArea set the line break to true).

I can adjust the height between two ListItems. This distance will also be applied to the height between the two rows of the same multi-line ListItem.

However, in my GUI, due to obsolete components and default line spacing in JTextArea, the two do not match. The difference is about one pixel, but on a large scale can accumulate and cause some problems.

So, is it possible to somehow set the string in JTextArea or in some way I can distinguish the space between two list items and two lines of the same list item?

I all use external libraries of any type and any tricks that may be required ...

+3
source share
3 answers

To override the JTextArea line spacing, look at PlainView (used to render PLainDocument).

The public void paint(Graphics g, Shape a) method has the following lines

  drawLine(line, g, x, y); y += fontHeight; 

This way you can adapt the y rendering correction y.

In the BasicTextAreaUI method to create a view. Replace it with your own implementation of PlainView

 public View create(Element elem) { Document doc = elem.getDocument(); Object i18nFlag = doc.getProperty("i18n"/*AbstractDocument.I18NProperty*/); if ((i18nFlag != null) && i18nFlag.equals(Boolean.TRUE)) { // build a view that support bidi return createI18N(elem); } else { JTextComponent c = getComponent(); if (c instanceof JTextArea) { JTextArea area = (JTextArea) c; View v; if (area.getLineWrap()) { v = new WrappedPlainView(elem, area.getWrapStyleWord()); } else { v = new PlainView(elem); } return v; } } return null; } 
+4
source

Manage the space between the lines using ParagraphAttribute SpaceBelow . You can do this with 4 lines of code or less (see Example below). You will need to use JTextPane to use these ParagraphAttributes attributes (but JTextPane and JTextArea` are so similar, you should not notice the difference).

 import java.awt.*; import javax.swing.*; import javax.swing.text.*; public class LineSpacingExample extends Box{ JTextPane modifiedTA; //Modify this to whatever spacing you want between the rows. static final float spaceBelow = 5.0f; //Font height - automatically calculated by code below int fontHeight = 0; public LineSpacingExample(){ super(BoxLayout.X_AXIS); //Demonstrating that the spacing is predictable final JPanel leftBox = new CustomBox(); add(leftBox); //Sets the amount of space below a row (only code you need to add) DefaultStyledDocument pd = new DefaultStyledDocument(); SimpleAttributeSet sas = new SimpleAttributeSet(); StyleConstants.setSpaceBelow(sas, spaceBelow); pd.setParagraphAttributes(0, pd.getLength(), sas, false); modifiedTA= new JTextPane(pd); add(modifiedTA); //Calculates the font height in pixels fontHeight = modifiedTA.getFontMetrics(modifiedTA.getFont()).getHeight(); } /** * @param args */ public static void main(String[] args) { final JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new LineSpacingExample()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } //EXTRA! Paints the left hand side box - to show that the spacing is predictable in pixels public class CustomBox extends JPanel{ public CustomBox() { super(); setOpaque(true); setBackground(Color.orange); } public void paintComponent(Graphics g){ super.paintComponent(g); int height = getSize().height; int drawLocation = 2; //To account for padding on the TextPane int row = 1; while(drawLocation < height){ //Drawing the text row background g.setColor(Color.blue); g.fillRect(0, drawLocation, 50, fontHeight); //Drawing the text row number g.setColor(Color.white); g.drawString(Integer.toString(row++), 0, drawLocation+14); drawLocation += fontHeight; //Drawing the space row g.setColor(Color.green); g.fillRect(0, drawLocation, 50, (int)spaceBelow); drawLocation += spaceBelow; } } }; } 
+1
source

You can try to use JTextPane instead of JTextArea and set the line spacing so that it looks like your ListItems.

 SimpleAttributeSet set = new SimpleAttributeSet(); StyleConstants.setLineSpacing(set, 0.5f); // <--- your value here textPane.setParagraphAttributes(set, true); 

This will not affect the line spacing of the existing text, so you must set the text later.

0
source

Source: https://habr.com/ru/post/1441144/


All Articles