Custom Swing JTextComponent

I want to create a JTextArea that looks like JTextArea, acts like JTextArea, answers like JTextArea, says like JTextArea, moves like JTextArea, etc., but is not JTextArea.

To do this briefly, I would like to create a custom swing component based on 100% on JTextArea. As soon as I do this, I can change the different, otherwise hard-set properties of JTextArea, and create my own JTextArea. There are no predefined rotation components that are designed the way I need them, but JTextArea is the closest, so I choose it.

I would like to change the line spacing of JTextArea. And no, I don’t want to use JtextPane, I tried it, it doesn’t work with my program, it calculates its position differently, it looks differently, and applying the JtextArea border is just useless.

I am not trying to extend JTextArea, I am trying to create a custom JTextArea, as in a custom swing component , with modified hard-coded properties that are not configurable when using JTextAreas methods.

However, I do not know how to do this. I searched it online, but there is only an extensive guide on how to create your own component from a package ...

Believing it will take a long time and not solve my problem.

The only thing I need to do is create a class (or several classes) that will contain everything that JTextArea creates. Start at the JTextComponent level and copy all the lower level classes that are used to create the JTextArea. I would also like to note that I am using the Nibus look, I think there may be some classes that must be included for a custom JTextArea to work correctly in this LAF.

I looked at the swing source code and it is full of everyone. Finding out which classes or parts of them are used when creating JTextArea will be a time-consuming nightmare, given that I do not know about the structure and mechanics of the kernel.

That's why I ask someone who has the knowledge to at least list the classes that I need to replicate JTextArea, and then I will figure out how to compose them.

Because if I start to study now the mechanics of swinging the kernel, it will take several days and weeks before I find out, but for those who know, it only takes a few minutes to list all the classes that I need to focus my attention.

I am trying to use a shortcut here. I don’t want to fully understand the swing, I just want this work to work. The default spacing is one pixel too low, and all I want to do is just make the pixel higher. I don’t want to know how the artist paints a component on the screen, I just want to know where he is called from and what he calls himself ...

Thanks to everyone who takes the time.

-2
source share
1 answer

I would like to change the line spacing of JTextArea

My first thought was that overriding javax.swing.JTextArea#getRowHeight would be enough. Javadoc clearly states

Defines the height value of the string. This default value corresponds to the font height.

Therefore, I hoped that by overriding this method, you would edit the definition and you would get a greater line spacing. Bammer did not work. A quick search about using this method in the JDK showed the same thing. It is mainly used to calculate some sizes, but, of course, is not used when drawing text inside a component.

Having examined the source code of the javax.swing.text.PlainView#paint method, I saw that FontMetrics used, and those that you can easily override in JTextArea . So the second approach was to extend JTextArea (bwah extending Swing components, but to prove the concept)

  private static class JTextAreaWithExtendedRowHeight extends JTextArea{ private JTextAreaWithExtendedRowHeight( int rows, int columns ) { super( rows, columns ); } @Override public FontMetrics getFontMetrics( Font font ) { FontMetrics fontMetrics = super.getFontMetrics( font ); return new FontMetricsWrapper( font, fontMetrics ); } } 

The FontMetricsWrapper class basically delegates everything except the getHeight method. In this method, I added 10 to the delegate result

 @Override public int getHeight() { //use +10 to make the difference obvious return delegate.getHeight() + 10; } 

And this leads to more spacing between lines (and a carriage too long, but this can probably be adjusted).

A small screenshot to illustrate this (not as good as some others, but it shows that this approach may work):

Difference between regular text area and extended one

A small disclaimer: this seems like an ugly hack and can lead to unexpected problems. I hope someone comes up with a better solution.

+6
source

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


All Articles