Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8771931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:54:51+00:00 2026-06-13T17:54:51+00:00

So, I have this JTexrtArea which is almost perfect for my needs. The only

  • 0

So, I have this JTexrtArea which is almost perfect for my needs. The only thing wrong with it is the line spacing. I can’t set it. (Why not JTextPane? Because spacing CAN be changed in JTextArea and JTextArea is way lighter thatn JTextPane, and I have a bunch of those in my program).

I have asked this question before, and this is the answer that I got from user StanislavL:


To override JTextArea’s line spacing take a look at the PlainView (used to render PLainDocument).

There are following lines in the public void paint(Graphics g, Shape a) method

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

So you can adapt the rendering fixing y offset.

In the BasicTextAreaUI method to create view. Replace it with your own implementation of the 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 grasp the general idea of what he’s telling me to do, but I don’t know how to do it.
Also, I wouldn’t like to override the default JTextArea “property”, I’d like to have a choice – to use the default one or to use a custom one.

Only change in JTextArea code would be from

y += fontHeight,

to

y+= (fontHeight +(or -) additionalSpacing).

How do I achieve this?
Which classes do I use/copy?
Where do I put them?
How do I make them usable?
How do I get the whole thing working?

If you think this is too specific to be useful, maybe someone could write a general tutorial on how to create a custom swing component based 100% on an existing one. Then someone could easely change some values to better adjust it to it’s needs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:54:52+00:00Added an answer on June 13, 2026 at 5:54 pm

    I am simply going to copy-paste my answer from your other question.

    I’d like to change the spacing inbetweem the rows of a JTextArea

    My first thought was that overriding javax.swing.JTextArea#getRowHeight would be sufficient. The javadoc clearly states

    Defines the meaning of the height of a row. This defaults to the height of the font.

    So I was hoping that by overriding this method, you would adjust the definition and you would get more spacing between the rows. Bummer, didn’t work. A quick search on the usages of that method in the JDK revealed the same. It is mainly used to calculate some sizes, but certainly not used when painting text inside the component.

    By looking at the source code of the javax.swing.text.PlainView#paint method, I saw that the FontMetrics are used, and those you can easily override in the JTextArea. So second approach was to extend the JTextArea (bwah, extending Swing components but it is for a proof-of-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 that method I added 10 to the result of the delegate

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

    And this results in more row spacing (and a caret which is way too long, but that can probably be adjusted).

    A little screenshot to illustrate this (not as nice as some of the other ones, but it shows that this approach might work):

    Difference between regular text area and extended one

    Small disclaimer: this feels like an ugly hack and might result in unexpected issues. I do hope somebody comes with a better solution.

    I personally prefer the solution StanislavL is proposing, but this gives you an alternative

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this form, in which i need to populate a combo box with
I have a JTextArea for which I have set word-wrap and wrap-style-word to true.
I have a JTextArea which has its text set to a string of information.
If i have something like this, where i can control the auto-scrolling using boolean
I have a JTextArea which is uneditable under a certain setting. However, under this
I have this class for my UI public class MyFrame extends JFrame{ JTextArea textArea;
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
Have this self-made slider: http://jsfiddle.net/wyc3P/4/ What it does: takes min and max values in
I have this string 2012-06-27 16:17:06 and I want to convert it to GMT
I have this application where I implement the ActionBar Fragment interface. Underlying the interface,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.