7

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.

2
  • 3
    Why don't you use a JTable? It seems more appropriate to display data in columns.
    – JB Nizet
    Commented Dec 16, 2011 at 11:43
  • 1
    I would rather go for a JTable that will give you the flexibility of playing around with column size and location etc.
    – GETah
    Commented Dec 16, 2011 at 11:50

4 Answers 4

9

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");
}
3
  • Thanks a lot. Thats what I was preciously looking for. Solves my problem. Cheers.
    – Subash
    Commented Dec 17, 2011 at 11:10
  • JTable may be fine, but sticking to JTextArea allows adding additional unformatted information to the same component as well as more tables if needed. Commented Dec 17, 2011 at 11:16
  • mono-spaced does it perfectly.
    – jmreader
    Commented May 11, 2017 at 17:56
8

Use a JTable instead (for what is apparently tabular information). See How To Use Tables for more details & working examples.

Table Sort Demo

0
6

You may use HTML with swing component or use JEditorPane.

JLabel jt=new JLabel();
jt.setText("<html>
            <table border='1'>
               <tr><th>No</th><th>Name</th></tr>
               <tr><td>1</td><td>Mr.A</td></tr></table></html>");
1

You can also change the font of the JTextArea if it is allowed in your problem

textArea.setFont(new Font("monospaced", Font.PLAIN, 12));
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.