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 6953715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:30:53+00:00 2026-05-27T14:30:53+00:00

I want to print Strings in JTextArea and align them properly. Its hard to

  • 0

I want to print Strings in JTextArea and align them properly. Its hard to explain so I will upload the screen shot of what I am trying to achieve.

Screen shot of what I have got so far

So Strings printed in each line are printed from Paper object which has parameters (id, title, author, date, rank). The data is read from a text file and is stored in a LinkedList using loadPaper() function.

Then displayPapers() function is used to display content of the Paper object to the JTextArea.
displayPapers() is listed below:

/** Print all Paper object present in the LinkedList paperList to textArea */
public void displayPapers(){
    // clear textArea before displaying new content
    displayTxtArea.setText("");

    Paper currentPaper;
    ListIterator<Paper> iter = paperList.listIterator();

    while(iter.hasNext()){
        currentPaper = iter.next();
        String line = currentPaper.toString();

        if("".equals(line)){
            continue;
        } // end if

        String[] words = line.split(",");
        displayTxtArea.append   ("  " 
                                + padString(words[0],30) 
                                + padString(words[1],30) 
                                + "    " 
                                + padString(words[2],30) 
                                + "  " 
                                + padString(words[3],30)  
                                + padString(words[4],30) 
                                + "\n");

        System.out.println(words);
        //displayTxtArea.append(currentPaper.toString());
    } // end while

    displayTxtArea.append("  Total " + noOfPapers + " entries!");

} // end showAllPaper

The padString() function adds spaces to the String so that all of them have same number of words. PadString() is listed below:

/** Add spaces to Strings so that all of the are of same number of characters
 *  @param str  String to be padded
 *  @param n    total number words String should be padded to
 * @return str  Padded string 
 */
private String padString(String str, int n){
    if(str.length() < n){
        for(int j = str.length(); j < n; j++){
            str += " ";
        } // end for
    } // end if
    return str;
} // end padString

I have worked on this for a while but still cant get the solution. As you can notice the above picture not everything is perfectly aligned as intended.

How do I align them perfectly so that it looks nicer? Thanks.

  • 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-05-27T14:30:54+00:00Added an answer on May 27, 2026 at 2:30 pm

    Output will be aligned “properly” in your JTextArea only if you use a mono-spaced font. “Andale Mono 14” for example would do the trick.

    Also, in order to make your life easier and avoid the padding hell, use String.format with it’s syntax.

    String format = "%1$5s %2$-40s %3$-20s";
    String someLine;
    while (whatEver...) {
       ... 
       someLine = String.format(format, aNum, aName, aDate);
       jTextArea1.append(someLine + "\n");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have so many strings and i want to print them all one by
I want to print an attribute value based on its name, take for example
I am storing some simple strings to a vector, and I want to print
I just want to know how to print out Strings in an ArrayList with
I want to print a list of strings like [hallo, world, !] using putStrLn
Ajax calls an array of four strings, I then want to print each string
Hai all, In my iPhone app want to print a series of strings in
I want to print list of strings like shown below. |Name|Country|Age| ------------------ |1 |USA
I have some comma separated strings and want to split them using cut command
I have a string: # print thestring abcd\t$500\n I want to extract the dollar

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.