Align rows in columns in JTextArea

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.

Screen shot of what I have got so far

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:

 /** 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 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.

+6
source share
4 answers

The output will be aligned "correctly" in your JTextArea only if you use a font with a single spacing. Andale Mono 14, for example, would do the trick.

Also, to simplify your life and avoid the addon, use String.format with its syntax .

 String format = "%1$5s %2$-40s %3$-20s"; String someLine; while (whatEver...) { ... someLine = String.format(format, aNum, aName, aDate); jTextArea1.append(someLine + "\n"); } 
+7
source

Use JTable (for what appears to be tabular information). See How to use tables for more details and working examples.

Table sort demo

+8
source

You can use HTML with the 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>"); 
+6
source

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

 textArea.setFont(new Font("monospaced", Font.PLAIN, 12)); 
+2
source

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


All Articles