So, I have this JTexrtArea, which is almost perfect for my needs. The only thing that’s wrong with him is the line spacing. I can’t install it. (Why not JTextPane? Since the CAN interval can be changed in JTextArea, and JTextArea is much easier than JTextPane, and I have a group of them in my program).
I already asked about this question , and this is the answer I received from user StanislavL:
To override the JTextArea line spacing, look at PlainView (used to render PLainDocument).
The following lines exist in the public void paint method (Graphics g, Shape a)
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; }
I understand the general idea of what he tells me to do, but I don’t know how to do it. In addition, I would not want to override the default JTextArea property, I would like to have a choice - use default or use custom.
Only a JTextArea code change would be from
y += fontHeight ,
to
y+= (fontHeight +(or -) additionalSpacing) .
How do I achieve this? What classes do I use / copy? Where do I put them? How to make them usable? How can I make it all work?
If you think this is too specific to be useful, maybe someone can write a general tutorial on how to create your own swing component based on 100% on the existing one. Then, someone could easily change some values to better adapt them to the needs.