Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - formatting string output to display in a TextArea
    primarykey
    data
    text
    <p>I've got 2 fields to display to a textarea in a simple java application I'm writing to learn the swing library.</p> <p>Basically, this small app has a few buttons I can click and the values for each button get stored in an ArrayList. When I press "calculate", I want to output the names, values, and a sum of the numeric values at the bottom of the output.</p> <p>For example, element 1 in the ArrayList would be an object with a name of "ItemOne" and a value of 12. Element 2 would be an object with the name "ItemTwoHasALongName" and a value of 15.</p> <p>I want to output the following to a textarea:</p> <pre><code>ItemOne 15 ItemTwoHasALongName 12 Total: 27 </code></pre> <p>I have no problem outputting the values, but I have trouble getting the formatting to be correct. Here's what I have:</p> <pre><code>for (MyItemObjects itemObject : objItemObjects){ this.billTextArea.append(itemObject.getItemName() + " " + itemObject.GetItemCost()); total += itemObject.getmCost(); } this.textArea.append("Total: $" + Total + "\n"); </code></pre> <p>Now, I know I'm not left padding the item cost in the above example. Whenever I try to, I get awful looking output that isn't correct. </p> <p>Here's an example of how I'm trying to format the output as a string, then I append the string to the text area. How can I have the item name be left justified, and the item cost padded left by as many spaces as necessary (based on the item name's length) to get the cost to be in line as a column for each line? </p> <pre><code>String strLine = String.format("%" + maxLineLength + "s%", itemObject.getItemName() + " " + itemObject.GetItemCost()); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload