I already asked here, so please have a read through here. This question is partially solved. How can I add \t spacing in a String that belongs to a DefaultListModel which belongs to a JLIst?
Now I have another problem:
Please look at this example first
// Declarations
String string1 = "Eggs";
String string2 = "Whole Chicken";
int quantity1 = 100;
int quantity2 = 25;
// HTML and PRE tags are added before and after string
StringBuilder builder1 = new Stringbuilder;
builder.append("<html><pre>");
builder.append(String.format("%s \t %d", string1, quantity1));
builder.append("</pre></html>");
StringBuilder builder2 = new Stringbuilder;
builder.append("<html><pre>");
builder.append(String.format("%s \t %d", string2, quantity2));
builder.append("</pre></html>");
// JList is defined, and the builder is added to the list.
JList<String> list = new JList<String>();
list.addElement(builder1.toString());
list.addElement(builder2.toString());
This is how it will display all the items
Eggs 100
Whole Chicken 25
What I would like to happen is that all the quantity variables are aligned rather than spaced based on the previous variable. Any ideas how I could make this happen?
Thanks!
==========================
Another suggestion would be if it's possible to define the minimum length of a string within a String.format()?
As @GhostCat said, using the width in the format string will solve your issue.
For more info, look at the documentation of the format string.
However, what you're trying to do makes me think that maybe, instead of using a JList, you could use a JTable?