问题
I've heard somewhere that it's not possible to add \t (tab) spacing in GUIs, is this true? Does \t only apply to System...println etc?
In any case, how would I be able to cause tabbed spacing?
Current Code and Preview of JList
Code | String.format("%s \t %s \t %s", string1, string2, string3);
Preview | string1 string2 string3
Expected Preview
string1 string2 string3
Other tests
I did try adding a space String between e.g.
String space = " ";
String.format("%s %s %s %s %s", string1, space, string2, space, string3);
But the spacing isn't consistent with multiple JList models.
Got a solution to this?
Thanks.
回答1:
JList supports HTML format, so you may use it that way :
DefaultListModel<String> model = new DefaultListModel<>();
StringBuilder builder = new StringBuilder();
builder.append("<html><pre>");
builder.append(String.format("%s \t %s \t %s", "str1", "str2", "str3"));
builder.append("</pre></html>");
model.addElement(builder.toString());
See : Can Items in a JList be formatted as HTML
来源:https://stackoverflow.com/questions/41138738/how-can-i-add-t-spacing-in-a-string-that-belongs-to-a-defaultlistmodel-which-be