3

Here is a picture of my JTextArea: enter image description here

Here is my code:

String display = "";
display = display + num + "\t\t" + name + "\t\t\t\t\t\t\t\t" +
        stocks + "\t\t\t" + req +"\n";

txtArea.setText(display);

What should I do so that the texts are aligned properly regardless of the length of characters of the words?

As much as possible I want to use JTextArea not JTable (since I'm not familiar with it yet) Thank you in advanced!

1
  • 3
    Use e.g. String.format("%20s %10s%n", value1, value2) to print values with a given column width. Commented Apr 20, 2016 at 14:56

4 Answers 4

2

Use a JTextPane instead of JTextArea, as that can do HTML. Then add an HTML <table>. Probably with some styles.

StringBuilder display = new StringBuilder("<html><table>");

display.append("<tr><td align='right'>").append(num)
        .append("</td><td>").append(name)
        .append("</td><td align='right'>").append(stocks)
        .append("</td><td>").append(req)
        .append("</td></tr>");

display.append("</table>");
txtPane.setText(display.toString());

This allows proportional fonts and styled text like bold, red, background colors.

1

As mentioned by @AndyTurner above your best approach is to rely on the String formatter to set the character width of each variable to print with also the ability to right or left justified. So in your case, as you left justified everything it could be something like that:

txtArea.setText(String.format("%-3s%-20s%-5s%-5s%n", num, name, stocks, req));

In this example I allocated 3 characters for num, 20 for name and 5 for stocks and req.

More details here

3
  • Answers the question but is not the proper way to solve the problem. Any time you have to pick a "magic number" you should be looking for a better solution. You need to pick a size for each column. But what if you have one value in the column outside the range? If you make the number too high then you have too much wasted space in the row and the user will have to scroll to see all the data. If its too small then the data will become unaligned.
    – camickr
    Commented Apr 21, 2016 at 14:50
  • That's correct except if the data comes from a relational database like most applications even nowadays because in this case you know the length of each field as you set it in your table definition so it can still make sense. As usual it depends on the exact context of the target application... Commented Apr 21, 2016 at 15:00
  • But the point is even if it comes from a database they will set a large number, like 50. This does not mean you want to reserve space for the large number because majority of the time you will probably only have 20. So you plan your view to display 20 (so you don't waste space in the view), but allow for the possibility of 50. Which is why something like a JTable is a better solution. You design the table so the default width of the column displays 20 characters. But then the user can temporarily resize a column to see the entire name when required.
    – camickr
    Commented Apr 21, 2016 at 15:12
1

I want to use JTextArea not JTable (since I'm not familiar with it yet)

Well, now is the time to become familiar with a JTable. Use the proper component for the job, that is why multiple components exist. Don't try to fit a square peg in a round hole.

A JTextArea is not the appropriate component for that kind of formatting.

Instead you should be using a JTable. A JTable is designed to display data in a row/column format. Check out the section from the Swing tutorial on How to Use Tables for more information and working examples.

If you must use a text component then use a JTextPane. You can manually set the value of a tab so all the text is aligned. The problem with this approach is again you need to determine what the size of each tab should be. So this means either you make a random guess at the size of each column or you iterate through all the data to determine the size. Of course this complicates the code. See: Java Setting Indent Size on JTextPane for an example.

Again, the better solution is to learn to use Swing how it was designed to be used.

1
  • Yes, I'll be practicing JTable now. Thank you!
    – user6230962
    Commented Apr 21, 2016 at 8:24
0
String display = "";
// just add values accordingly the best way to get the result you 
// want is to mess around with formatting until you have the values
// where you want them in the textfield.
display = String.format("%20s %10s%n", value1, value2);

//display = display + num + "\t\t" + name + "\t\t\t\t\t\t\t\t" +
//    stocks + "\t\t\t" + req +"\n";

txtArea.setText(display);