I want to print the lines in JTextArea and align them correctly. It is difficult to explain, so I will upload a screen shot of what I'm trying to achieve.

So, the lines printed on each line are printed from the Paper object, which has parameters (id, title, author, date, rank). Data is read from a text file and saved to LinkedList using the loadPaper () function.
The displayPapers () function is then used to display the contents of the Paper object in JTextArea. displayPapers () is given 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 they all have the same number of words. PadString () is shown below:
private String padString(String str, int n){ if(str.length() < n){ for(int j = str.length(); j < n; j++){ str += " "; }
I have been working on this for a while, but still cannot find a solution. As you can see above picture, not everything is perfectly aligned for the purpose.
How to align them correctly so that it looks better? Thanks.
source share